Step 13 — Loading Scenes by Restart or New Game

Nicholas Lim
3 min readMay 6, 2021

So you’re at game over, and you want to have another go. Let’s restart upon pressing R.

Same as the game over text, create another Text object for the restart prompt. Have it appear at the end of the game together with the game over. This time we’ll need to communicate with a Game Manager that oversees the state of the game. But first the UI Manager script is updated.

UI Manager script

Apart from knowing when to enable and disable the restart prompt, this will also call the GameOver method on the Game Manager object.

In the Hierarchy create an empty object, call it GameManager and give it a script of the same name. It’s a very simple script.

GameManager script

All the Game Manager does is keep track of whether the game is over with a bool. When the UI Manager calls GameOver this will become true, and then when you press R it will restart the Scene, which is also the whole game in our case.

To know what integer a particular scene is, you go to File → Build Settings and look at the Scenes in Build.

If we are only adding the Game scene into the build then its integer value is 0, however, we can also load the Game scene from a Main Menu scene.

Create a new Scene called Main Menu. Add desired Image and Text objects, but in particular we want a Button object. First however, we add a script to the Canvas called MainMenu, and all it contains is:

MainMenu script

We load Scene 1 because when we add the Main Menu scene to the Build Settings that will be its order in the Scenes.

Back on the Button object, we look at its OnClick() section.

We tell it what object to refer to (Canvas in our example) and then find the LoadGame method.

Now when we press the New Game button in the Main Menu, it will take us straight into the game.

--

--