Building off the previous article about a Game Over Screen, how about we add the ability to restart the game once the player looses all his lives. This can be done by simply reloading our given scene.
What is a Scene ?
The place where you work on in Unity is called a scene you can have multiple scenes in a game. Scenes are basically assets, by default one would be on a Sample Scene. You can create a new scene by doing the following,
Next thing to do is to add our scene to our project build, this can be done by doing the following,
Now let’s create a UI Text which will prompt us on what to do in order to restart the game,
Upon completion you can edit the newly created Text object to get your desired result.
Now here, since we wish to load a scene, it doesn’t make sense to have the UI reload a new scene it also doesn’t make sense to have the Player load the scene either. So let’s create a Game Manager which will manage everything related to our game which doesn’t fall under any other categories like in this case Loading a scene.
Here GameManager is an empty game object which contains a GameManager behavior script.
The Game Manager behavior is as follows,
Loading a scene is possible through a Unity package called SceneManagement so it is important for us to import that package into the GameManager. This will enable us to use the methods available in the SceneManagement package.
This enables us to use LoadScene method which is responsible for loading the given scene.
Where SetGameOver() is called in the UpdatePlayerLives method present in the UIManager script.
where _restartUI is the Text game object variable we had created with the intention of letting the player know that he has an option to restart the game when he presses the desired key. _gameManager is the variable which stores the reference to the GameManager script.
This gives us the following result,
Thank you for reading, more to come!!!