Loading Scenes in Unity

Trigger with a key press

Your player is having an awesome time playing your 2D Space Shooter game. Eventually they loose all their lives. They would like to play again, so let’s give them this option. When the Game Over sign is flashing on the screen, lets provide the option of playing again by pressing the ‘R’ key on the keyboard.

Option to replay level

As in the Game Over text, we need to create a new UI Text object in Canvas. Rename this to Restart_Text.

Creating Text object

Change text to Press ‘R’ to restart level, set color to white

Resize (don’t forget the overflow) and position on the screen with an anchor to bottom center.

Select the text object and place it where you want it on the screen.

We need for this text to appear when the Game Over sign appears. So let’s go into theUIManager script.

We need to connect this text object with the Canvas. So we need to create a serialized variable of type Text. Lets name it _restartText.

Back in Unity, select Canvas and notice that there is now a variable called Restart Text in the UI Manager script with nothing in it. Drag the Restart_Text object into this slot.

Now that they are connected, we need for the text to appear when the Game Over text appears, and at the same time make sure it does not appear at the beginning of the game.

In UIManager script, in the Start method, we can set the text object off.

Turning of restart level text

In the GameOver method, which is where we start the Game Over coroutine, we can have the text appear.

Activating Text object

With this, the text will now apear at the same time as the Game Over flickering text.

Restart text now appears at Game Over

The Restart Text appears but it is not functional. If you press the ‘R’ key on the keyboard, nothing will happen.

When expecting input from the player, ie a keypress in this example, it is best to create a new Game Object with it’s own script to handle this.

Lets create an empty game object in the Hierarchy and name it Game_Manager.

New Empty Game Object Game_Manager

We need to create a new C# script. Name it GameManager and drag it onto the Game_Manager object.

In order to load or reload a scene in Unity, we must first add our scene to the Unity Build Settings.

Open up File, Build Settings and drag your scene or scenes onto the Build Settings window.

Adding scene to Build Settings

Notice that each scene is assigned a number in front of it. This is an index number. the scenes can be called either by their name or their index number.

Scene indexing

Now lets open the GameManager script.

First of all, in order to load a scene, we need to use the SceneManager.LoadScene(). This isn’t available in the default Unity script. We need to add a new UnityEngine at the top of the script.

UnityEngine.SceneManagement

This will import the needed classes and methods we need.

Somehow we need to let the system know that the game is over in order to load a new scene. To do this, we will create a new boolean variable.

The default state of a bool variable is false. So, we need a way to make it true when the player looses all their lives. Lets write a method to handle this.

Setting boolean variable to true

In the Update method we can constantly check if the player has pressed the ‘R’ key AND if the _isGameOver variable has been set to true.

Checking if ‘R’ has been pressed and _isGameOver is true

This script’s purpose is to load the new scene for the player. We need to call the GameOver method from the UIManager script in order to activate it.

In the UIManager script, we need to connect the two in order to be able to call the GameOver method. First we need to declare a GameManager variable.

GameManager type variable

In the UIManager script’s Start method, we need to assign the location of the Game_Manager object and then check and make sure it did not come back null.

Assigning Game_Manager script component

now that we have assigned the GameManager script location, we can call the GameOver method. We do this in the GameOverSequence method.

calling the GameOver method

Now, when the player wants to replay the game when they loose all thier lives, they can press the ‘R’ key on thier keyboard and reload the scene, resetting score and lives.

Pressing the ‘R’ key to Replay Game

--

--

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
Marc Frappier

Marc Frappier

Poultry Scientist by profession, Computer Scientist at heart. After many years in the poultry business, I have decided to give my passion for technology a shot.