How to Load Scenes and Implement a Restart Game Feature in Unity — Part 02

Chris Hilton
3 min readJul 9, 2021

--

Objective: The follow up article to loading scenes in Unity and implementing a Restart Game feature!

Continuing on let’s now look into the Game Manager and why we are setting this up and what it will be controlling.

GameManager

The Game Manager is going to be controlling the game state mechanics. E.g. In our case it will be controlling the Restart Game mechanic.

We need to create a new empty object in the Hierarchy and rename to “Game Manager”. Next, let’s create a new script and call it ‘GameManager’ and add it as a component.

Within this script, we need to input from the user that the ‘R’ key has been pressed and we also need to control when the user can press this key:

To start with, we need to access the namespace: UnityEngine.SceneManagement as this library contains the ‘SceneManager’ class that we are going to need to load the scene later on.

Next, we have created a trigger switch which is going to determine when the user can press the ‘R’ key and restart the game by loading the scene again. 2 conditions must be met, firstly that the ‘R’ key is pressed and secondly, when the isGameOver bool is set to true. This only takes place when the GameOver() method is called from the UIManager when the player has died.

Build Settings

Once these conditions have both been met then the game will LoadScene(0) from the Build Settings menu in Unity. The ‘0’ is the index number that the scene receives when we ‘add a scene’ to the Build Settings:

When we add the next scene it will be given an index of 1 and the next scene will be 2 etc etc…

Alternative LoadScene Option

Unity also allows you to load scenes by the name of the scene (via a string) instead of using an index number. Therefore we can replace the (0) with the string text (“Game”) and it will load the scene as well!

This approach is slightly more performance intensive (using a string) and it is recommend to use index’s where possible.

--

--

Chris Hilton
Chris Hilton

Written by Chris Hilton

Passionate Unity Game Developer

No responses yet