Objective: Allow player to restart game after the game is over.
First instruct the player how to restart the game by adding another UI text element, named Restart_Game_text, with the settings shown below. Set the anchor point in the Rect Transform component to center bottom and adjust the position and box size using the scene view. Type instructions into the the Text component along with the color, white, and font size, 28. Set horizontal and vertical overflow to overflow. Finally, uncheck the box next the Restart_Game_text name so that it is in active.
Where to put the code for the actual input? While player inputs are often in the Player script, the player has already been destroyed. The UIManager script is what triggers the game over text. The UIManager needs to know when the game is over so create a _gameOver Boolean set to false at the beginning of the game. The instructions, child 3 of the Canvas, will become active in the UpdateLives() function if the player dies, CurrentLives ≤ 0, along with the game over notification. The _gameOver boolean is also set to true in this function.
Finally, we add the code for the player input within the UIManager. This code checks for the player pressing the R key, but also checks that the game is over. Both conditions are necessary, otherwise the player can restart the game in the middle of the game. The SceneManager.LoadScene(“Game”) reloads the current game, but to work must declare the using UnityEngine.SceneManagement namespace library at the beginning of the script.
The scene reloads and resets as if play was pressed in the editor, as shown below. Game over dude… restart!