Loading Scenes in Unity

Ahmed Mubarak
2 min readJul 4, 2023

--

Objective: We want to be able to restart our game after death, which means learning about loading Scenes in Unity.

Unity has a built-in system for handling loading Scenes; all we have to do is call the right method from the appropriate class. Good practice for handling Scenes is to create a GameManager class whose sole purpose is this. The first thing we need to do in this class is import Unity’s Scene Management system:

In this case, we want to give the player the option to restart the game after they die, so we need a boolean variable to know when the game is over. And we want the ‘R’ key to restart the game. These two functionalities are demonstrated by the following code:

To restart the game, we simply load the Scene from scratch. But in order to do this, we need to add the Scene to our Build Settings so the compiler knows what Scene to load:

Note the index of 0; this is the Scene’s placement in our game but if you only have one Scene this number will always be 0.

Now we can load the game using Unity’s LoadScene(sceneBuildIndex) method, which takes the Scene index as a parameter:

This will restart the game when the player hits ‘R’ but only if the game is over!

--

--