Loading Scenes in Unity
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.
As in the Game Over text, we need to create a new UI Text object in Canvas. Rename this to Restart_Text.
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 the UIManager 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.
In the GameOver method, which is where we start the Game Over coroutine, we can have the text appear.
With this, the text will now apear at the same time as the Game Over flickering text.
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.
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.
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.
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.
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.
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.
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.
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.
now that we have assigned the GameManager script location, we can call the GameOver method. We do this in the GameOverSequence method.
Now, when the player wants to replay the game when they loose all their lives, they can press the ‘R’ key on their keyboard and reload the scene, resetting score and lives.