My 90 day journey becoming a Unity game developer: Day-25
Objective: Create a Restart level when the player press a special key.
To start, we will create a game object inside the Canvas to show the text that will tell the player to press the ‘R’ key to restart the game. We will name it Restart_Text and do the same thing we did setting up the Game Over text. The only thing that will be different is the font size which will be a little smaller.
In the UIManager script, create a new variable name restartText which will be used to show the text on the screen telling the user what to do next when the game ends.
Set the restartText game object to be turned off when the game starts.
Drag the restartText game object over to the restart text variable within the UI Manager component located on the Canvas.
When we go inside the UpdateLives() method to show the restart level text the method can become a little cluttered with adding more code to this method. Instead, let’s create a new method name GameOverSequence() and add the code from if lives equal zero to it. Then inside the if statement add the GameOverSequence() method.
Insert the restartText game object and set it to turn on when the method is called.
Now we want to get user input to restart the level by pressing the ‘R’ key. To do this it’s best to create a Game Manager to handle user input for deciding what to do next with the game. Create a game object which will be the Game Manager itself, and create a Game Manager script to handle the user input.
Over in the Game Manager script, create a variable isGameOver which will be set to false. Then create a method name GameOver() where the variable is set to true when the method is called upon.
Inside the Update() function, have the user press the ‘R’ key if the game is over to restart the game. To use the SceneManager function make sure to add the using namespace called SceneManagement at the top. Then inside the if statement add the LoadScene() function to the SceneManager with the string name of the scene to load that particular scene.
Now head up to File and select Build Settings. Inside we will click on Add Open Scenes to create a new scene that will represent our game level. If you look carefully you will notice that the new scene is added with a number assigned. It’s better to use the number assigned to the scene than using the name of the scene itself. That’s because the game will load the scene using numbers faster than using the scene’s name.
Here I changed the LoadScene parameter from the name of the scene to the number assigned.
Back in the UI Manager script, we will create a variable that will be the reference to the Game Manager script. Inside the Start() function, use the variable to access the Game Manager’s component. Also, it’s good practice to check if a component isn’t NULL when we’re trying to access it.
In the GameOverSequence() method, add the gameManager variable and access the GameOver() method to set the game is over to true.
This is what will happen when the ‘R’ key is pressed.
Next we will work on a Main Menu.