Unity: Loading scenes to restart the game

So our player has met an untimely demise. Let’s give them a way to try again and restart the level.

Ian Plumpton
2 min readJun 21, 2021

We start by adding instructions to press ‘R’ to restart the game in the form of a new Text object which we set to appear when the player dies. See this article for more info on this.

We also add a new empty object called GameManager and a GameManager script to assign to it.

The entire script looks like this. We have a bool, _isGameOver which defaults to false. In Update we look out for this bool to be true and for the ‘R’ key to be pressed, at which point we use use SceneManager.LoadScene() (remember to include using UnityEngine.SceneManagement; for this) and pass in scene zero which is currently our only scene. Finally we have a GameOver() method to set the bool to true when called.

In the UIManager script we add a variable reference to the GameManager and update our OnPlayerDeath() method to call GameOver().

In our build settings (File -> Build Settings) we need to make sure we have our scene added as scene 0.

Now when the player dies, we get the instructions for restarting and hitting the ‘R’ key will re-load the scene to start the level again.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ian Plumpton
Ian Plumpton

Written by Ian Plumpton

Software developer in the field of Unity and C#. Passionate about creating and always driven to develop.

No responses yet

Write a response