Restarting a Level in Unity

Ryan McCoach
5 min readApr 17, 2021

The dreaded Game Over. When you see that on the screen, it just taunts you to play again immediately. In this article, I will go over how the player can simply hit a key to restart the level. Game On!

First, we will need to give the player instructions on what key we want them to use to restart the level. We are going to add TEXT UI element to our canvas. Once we have formatted the text and positioned it, we will want to deactivated, so it doesn’t appear on the screen until we want it to.

In the UIManager script, we need to create Text variable to hold the Restart Level text GameObject and give it a Serialized Field attribute, so we can assign it in the Inspector.

In making sure the Restart Text is not active when the game starts, we want to SetActive to false.

Since the Game Over part of UIManager script is growing, we will create a method to house it all making it easier to read and debug.

We will call this GameOverSequence method when the Player’s lives are equal to 0. In this method, we have the Flickering GAME OVER text and we will want to add the activation of our Restart Text.

In this GameOverSequence method, we could add the restart level functionality but that is really stepping outside of what the UI Manager’s purpose is, so we will need to create a new GameObject that will handle the state of the game. We will call it Game Manager and create a script for it.

The first thing we are going to add is a boolean variable to check if the game is in fact over; the player’s is out of lives. Of course, we want this to start as false.

On the GameManager script, we need to create a method that will set the isGameOver to true so we know when the program needs to check for the Restart key (‘R’ Key). The Game Manager does not know when the Player is out of lives, so it will have to talk to another script that does, so this method will need to be public.

The script that does know when the player is out of lives and runs the Game Over Sequence is the UIManager.

We need to let the UIManager know the GameManager exist, so we will create a GameManager type variable.

Use the the GameObject find line, get the GameManager component and store it into our _gameManager variable. Of course, we will NULL check to make sure we actually got the Game Manager component.

Now that we have our Game Manager handle, we can call the Game Over method from Game Manager script when we run the Game Over Sequence in the UIManager script.

Once the Game Manager know that isGameOver is true, we can check for the Restart Key being pressed.

If both of these conditions are true we can reload the level or scene. In reloading a scene, we will need access to the Scene Manager. We will need to tell our Game Manager script we need access to this library.

Once we have access to the Scene Management library we can use the Scene Manager to load a certain scene of level. Before we do that, we need to add our level or current scene to the build. In the Build Settings, click on “Add Open Scenes” and it will appear in the Scenes in Build box. Take notice of the number it is give, which in this case is 0. This is the scene index and we will need this integar to tell the Scene Manager, which scene to load.

Now that we have the scene added to the build, we can Load Scene and pass in the number of the scene we want to load, when our IF statement of the Restart key is pressed AND isGameOver is true.

--

--