Loading Scenes In Unity

Austin Hall
8 min readOct 12, 2022

--

Objective: Press the “R” key to reload the level scene.

In this article we are going to take a look at how to create functionality to restart the game using the “R” key.

Our first step is to provide instructions to the player for restarting the game. We already have our game over text, let’s create another text object and add the instructions.

To do that, we right click on the canvas, scroll down to UI, and select TextMeshPro from the menu.

We will call this object “Restart_text”. Uncheck the box to make it inactive and use the text field to provide the instructions.

Use the TextMeshPro component settings in the Inspector window to adjust the size and color of your text. In the Scene view, you can adjust the position like we did with our game over text.

NOTE: Make all of the necessary adjustments before setting the object to inactive, otherwise you won’t see the text in the scene. And don’t forget about wrapping and overflow.

The next step is telling our UI manager about this text object so that it can enable text at the correct time.

To do that we start by creating a new variable in our UI manager script. we will call it restart level text, and we will serialize the field. Be sure to follow current naming conventions.

Going back to Unity, we can drag the text object into the reference box. With our canvas selected click on, and drag the restart text object to the corresponding reference box on the script component in the Inspector tab.

So, when do we display the restart level text? That will happen when player lives is equal to zero.

We are getting to the point where we are doing a bunch of things when it’s time for the game to be over. At this point, it would make sense for us to create a separate method for our game over sequence.

Game Over Sequence Method / Method Call When Lives = 0

In our game over sequence method, we set the “restart level text” game object to active during the game over sequence. But, to make this function properly we have to make sure that on start, that component is set to inactive.

To do that, we simply use the .SetActive() function on start, and set it to false.

At this point, when the game is over, the text will appear on the screen with the game over text.

Now that we have the functionality built for our restart level text, we need to develop the logic that connects our player input. In other words, we need to figure out how to make the level actually restart when the player pushes the “R” key.

Our first thought should be “Where does user input belong?” Does it make sense to have it in the UI manager? The answer to that question is no. Typically, in a video game, you will have something you design called a game manager. The game manager is responsible for managing the state of the game, such as, is the game over. It will also control things like user input.

First, in the Hierarchy, let’s create an empty object and call it game manager.

In our scripts folder, we are going to create a new script and label it “GameManager”. Drag and drop it into the game manager object. Double click on the script to open it in MS visual studio.

NOTE: I use a naming convention. for objects in my scene, I add an underscore Game_manager), which makes it easer to read. But in my scripts I do pascal casing (GameManager).

NOTE: You will notice that a gear icon appears in place of the typical C# script icon. this is a built-in feature in Unity to identify the Game Manager script.

In our game manager script, we won’t need a start method, but we will need an update method. We’ll start by creating a private bool variable and name it “_isGameOver”. We are also going to create a game over method.

The default state of the bool will be false, and in our game over method we will set it to true. If we add the serialize field attribute to the variable we can see that in our inspector.

Now, lets think about the logic for our game over method. If the “R” key is pressed, then we want to restart the current scene.

Unity has something called scene management. There is a Scene Manager class, and inside that class is a method called load scene.

In the description we can see that this method loads scenes by name or index in the build settings.

For further clarification, you can refer to the Unity scripting API, where they have multiple examples. For our purposes we only need to be concerned with how the method is used to call a specific scene (See example below).

Code Example From Scripting API

if we go into the code and type SceneManager.LoadScene, nothing will happen. The reason for that can me found in the code example above. It is always important to read the name space libraries that are being used in a specific example. in this case we are looking for “using UnityEngine.SceneManagement”. this library is required to use the Scene manager class and load scene function.

Using an if statement we can create the logic we need. Remember we only want the scene to reload if the game is over AND we press the “R” key.

When we call the load scene method, you can see that we have the option to call by an int, which is our scene index from the build settings, or by name which is the name of the individual scene we want to call.

If we want to us the scene name, we can go to our scene folder and find the name of the scene we want.

Then we go back to our game manager script and insert the name.

if you were to go back to Unity and test this now, you would get an error. That is because we have not added the current scene to the build settings yet.

To do that we go to file, Build Settings, and Add Open scenes. If you look at the “scenes in build” section, on the right-hand side you will see a number. that is the scenes build index, which in this case is zero.

If we wanted to use the index instead of the name, we could load scene zero. Technically speaking this is a much quicker and more efficient way to load scenes. Searching for something using a string is always slower.

When we go back to Unity and run our game, we find that the “R” key is not reloading the scene. How do we debug this? If we look in the inspector we see that “_isGameOver” never set to true.

Now, we need to go back to our game manger script and find out if our GameOver() method is being called. to do that we will use Debug.log to print a message in our console when the method is called.

When we test it again, we find that the method never gets called.

To call the GameOver() method, let’s go back to our UI manager script. We can create a handle to our game manager, and call it “_gameManager”. In our Start method we use get component to assign the game manager to our handle.

It is always good practice to null check when using get component. We will perform this operation in the start method, immediately after assigning our handle.

Now we can go to our game over sequence and call the game manager’s game over method.

Save your scripts, run the application and you should be good to go.

“R” Key Pressed To Restart Level

--

--

No responses yet