Loading Scenes in Unity

This article will go over creating a “restart level” feature. I will use my space shooter game to illustrate.

Object: Allow the player to restart a level after losing all lives.

First, we need to create a text element on the Canvas that will instruct the Player on what to do to restart the level. I will use “Press the R key to restart level"

Right-click on the Canvas, select UI then text, I will rename to “Restart_Level_text”. change the text to “Press the R key to restart level”, change the size, change colour to White, change vertical overflow to overflow. stretch it out and move to the desired location. finally, anchor to the bottom middle.

Next, we need to update the UI Manager script with a reference to the restart level text.

Then with the Serialized Field attribute, we can assign it in the Inspector

Now that it is assigned in the Inspector, we can work on the logic to have it Display when the Player is out of Lives and the Game is Over. First, we will make sure it is off(False) at the start of the Game. I have added it to the Start method. As shown below, it is the last line of code.

We also need to have the logic to turn it back on(True), when the Player is out of Lives and the Game Over message is playing. I have added the code in the GameOverSequence method. As shown below

Next, we will create a Game Manager, this will control the state of the game. First, we create an empty game object then attach a script to it.

In the GameManager script, we will need to add a Unity library that will allow us to manage the game scenes.

In the Game Manager script, we will create a bool to determine if the game is over(_isGameOver), this will be false by default. In the Update method, we will check for the R key being press and if the _isGameOver is True. If they are both true then we load the scene using the SceneManager(SceneManager.LoadScene(0)). This will load the scene with an index of 0. Which in my case is the Game scene.

That last thing we need is a method to set _isGameOver to True. I created a method called GameOver().

We will need to add the Scene to the build settings. Click file →Build Settings → Click on add open scene. This will add the open scene to the build and assign an index value. The index value is used to determine what scene to Load, As shown above, LoadScene(0).

Ok Now that the Game manager is set up and the build settings are updated with our scene, we will need to access it from the UI Manager when the Player Lives are zero to tell it that the Game is Over. In the UI Manager Script, we will create a reference to the Game Manager.

Now that we have a reference to the GameManager we can assign it in the Start method GetComponnet, as shown below. also, remember to null check the game manager.

In the GameOverSequence method, I added a call to the GameOver method in the _gameManager.

Ok now we have everything in place, let's see it in action!

Restarting the Game

Next article I will show you how to add some visual effects such as the enemy exploding and the Player taking damage as you see above. See you there.

Happy Coding!

--

--

--

Unity / C# Game developer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Pennsylvania driver license Psd Template

you have need to know about prestashop ?

A function can be defined as a block of organized, reusable code that is used to perform a single…

Sample Sparkjava Program

Synchronizing states using Mutex vs Channel in Go

[ML UTD 22] Machine Learning Up-To-Date — Life With Data

CIID Weeks 8 & 9: Physical Computing

What is Vector in C++? Get started in 5 minutes

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Bill Rislov

Bill Rislov

Unity / C# Game developer

More from Medium

Day 103: Creating A Ledge Grab in Unity Part 1

More Productive Editor Layout in Unity

Adding a thruster speed boost on keypress

Upgrading Controls on a Forklift to the new Unity Input System