Loading Scenes in Unity

Kyle W. Powers
5 min readApr 18, 2021

In the last article, we created a GAME OVER that would appear and flicker when the Player dies. Now we will make a restart function from the Game Over screen and a Main Menu to load the game.

The first thing to do is create a Text element and name in “Restart Text”, this is what will appear to let you know you can restart the game. In the Text field, type in “Press ‘R’ to Restart” and adjust it till it looks how you want.

Since it does not make sense to have user input in the UIManager script, we will create an Empty gameobject named “Game Manager” and create a script called “GameManager”, and attach it to the Game Manager gameobject. That is where we will handle the user input for the restart key press.

The SceneManager class handles loading Scenes in Unity. To get access to it, we need to add a “using UnityEngine.SceneManagement” namespace to the GameManager script.

Next, create a private bool named “_isGameOver” with a false value; this is how the GameManager will know if you can restart the game. Then create a public method called “GameOver” and have _isGameOver set to true in it.

In Update, make an if statement checking for the R key to be pressed down and _isGameOver to be true. Inside the If statement, create a local int named scene and have it equal SceneManager.GetActiveScene().buildIndex. That will find the currently active scene’s index number and tell the game to reload that specific scene. Next type SceneManger.LoadScene and pass in the scene int we got for the currently active scene; this will reload the current scene.

In the UIManager script, create a private Text variable named “_restartText” with a null value; this will reference the Restart Text. Then create a private GameManager with a null value; this will hold the reference to the GameManager. Next, in Start, retrieve the reference to the GameManager using GameObject.Find looking for the Game Manager and get the GameManager script component, and then null check it. Also, set the _restartText gameobject to false to hide it till we activate it.

In the UpdateLivesImage method, in the if statement, set the _restartText gameobject to true and call the GameOver method on the _gameManager. That will make the Restart Text appear and tell the Game Manager the game is over.

In Unity, hook up the Restart Text gameobject to the Restart Text field on the Canvas.

Now in Play Mode, you can see that when the Player dies, the Restart Text appears, letting you know you can restart the game by pressing the R key, and it restarts the game.

For the Main Menu, create a new scene and name it “Main_Menu” and then open it. In the new scene, create an Image UI element, then drag in the main menu image, adjust the size, add the background image from the game, set the Main Camera from Skybox to a solid color, and make the color black.

Next, create a new script named “MainMenu” and attach it to Canvas. Then create a Button element and call it “New Game Button” and change the Text child to say “New Game”.

Now we need to add the Main_Menu scene to the Build Settings and make sure it is the first one in the Build Index to be loaded first when we build the application later.

In the MainMenu script, add the “using UnityEngine.SceneManagement” namespace to use SceneManager to load the Game scene. Then create a public method named “LoadGame” that the UI Button will call. Inside the method, have the SceneManager load the second scene in the Build Index, which is 1 since it starts at 0, and this is the Game scene.

In Unity, select the New Game Button and add an On Click event. Then drag the Canvas into the field and select the MainMenu script’s LoadGame method as the function. Now, whenever the button is clicked, it will run that method.

With that, the Main Menu now works and sends you to the Game scene.

In the following article, we will give the enemies an explosion visual effect when they get destroyed.

--

--