In my previous article, I created a retro-styled game over text title for when my player dies. It makes sense that I begin by giving the user the option to restart the game.
Restarting the game will take a few steps.
Create functionality for restarting the level when the player dies.
I have to determine which Components will be responsible for handling this behavior. It makes sense that I make my GameManager handle all game-related events.
Previously, I had set up my GameManager script to control a bool for the game state.
When my Player dies, it does two things. It calls the GameManager’s public method and sets the game over boolean to true. It also calls the UIManager to make sure it activates the game over Text.
Now we know the GameManager is up to speed. Let’s begin by adding code to reload our scene.
The first step is to add Unity’s SceneManagement namespace to our using directives at the top of the GameManager script. With this namespace, we will have access to all the Types and methods required to handle our game scenes.
Since I am working on a desktop version of this game, I have decided that the best solution is to use the ‘R’ key for reloading my scene. In the GameManager’s
Update() method, I will check for user input. But that’s not enough. If the user hits the key accidentally during gameplay, they will restart the game and lose all progress.
Fortunately, I have a bool in place for just such a thing. I can use the _gameOver bool to check along with the selected key to make sure the game only restarts if it has already ended.
Let’s take a peek at the method for loading a scene.
The first version of this method takes an int which represents the scene’s build index. More on that shortly. The second one takes the scene’s name or path as a parameter in string format.
So what is a build index? You can find the list of scenes in your build by going to File > Build Settings.
Each scene in that list will have a number based on its position on that list.
As of now, I have not gotten to the point of adding scenes to the build. I will cover this at a later date.
Since I don't have a scene build index, I will have to use the active scene’s name as the parameter for the
Unity’s SceneManagement provides us with a helpful public method we can use.
This method will return all the information regarding the active scene. By using the dot(.) operator, we can access members of that class like the scene’s name.
The resulting code is the following:
All that is left to do is to create a visual prompt for the user. I will create a new Text Element on my Canvas for this purpose. Once I have set its position, color, and scale, I will make it a child of the Game Over Text. I will also disable the Text Component of this last GameObject.
In my UIManager, I will then add a reference to this new Text Element. I can use the OnPlayerDeath method to enable it when my player dies.
And that will do it. I now have a way that allows my player to restart the game if they die.
The result is the following.
SceneManagement can get far more complex than this last example. But understanding the basics and knowing where to find the information can help us build robust code to handle our game’s scenes. I hope this quick overview has been helpful to you. I urge you, as always, to read Unity’s online documentation.