Loading and Re-Loading Unity Scenes

As I mentioned last time, it would be great to allow the player to restart the game once they’ve lost. We can achieve that using Scene Management. The plan here is to simply reload the game and everything in it, which will reset everything back to the way it started.

Here we add a prompt that tells the player that they can press ‘R’ to restart. (See my UI article to learn how to make this text appear)

To make this happen we need some setup in the Input Manager. We can create a new input field called “Restart” that will activate the restarting behavior. This activates when either the ‘R’ is pressed on the keyboard, or the “start” button on a compatible controller. (Here’s more on how to use the Input Manager)

We also need to define what scene we want to load. We can find this under File -> Build Settings. After adding the open scenes to this build, we can see that the integer ID of this game scene is ‘0’. We can use this later to load the scene.

Now, lets make a new Game Manager empty object and a script with the same name. This script will control vital game functions like resetting. To do this, we need to include the line “using UnityEngine.SceneManagement” to allow scene manipulation like loading. In Update, we check if the “restart” button is pressed and the game is over, and then load scene ‘0’ which is the game scene.

Three new lines In the Start() method

Now we just need to make sure “GameOver” is called somewhere. We can do this in the UIManager class, since it already calls its GameOverRoutine when the player’s lives hit zero. Here we use the new _gameManager variable to communicate with the GameManager script.

That completes the restart functionality! Just press ‘R’ and the game starts over.

Now lets go even further and make an entirely new scene: the Main Menu! We can create new scenes using the File menu at the top.

We can use this same file menu to save the scene as “Main_Menu”. After adding some images and text, here’s the completed main menu.

Now that we have this new scene, we’d like to have the game it first. Lets re-order the scenes in build setting by deleting the game scene that’s there are dragging in the scenes, main menu first.

We’ll also need to add a MainMenu script to the canvas in the new scene. Its purpose is to check for player input and switch to the game scene.

Since the scenes switched IDs, we’ll also have to update the GameManager so that it loads the game scene when restarting.

Now we have two fully functioning scenes and a way to load and reload the game scene.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Blake Zoeckler

I’m a passionate and talented software engineer seeking an opportunity in game development.