Creating a Game Over Screen in Unity
For today’s article, I’ll be going over how I created the game over screen you see above.
Setting up the Scene
The first thing I do is I create a Canvas game object by right clicking in the Scene hierarchy and selecting Canvas:
Unity will create the Canvas game object alongside the EventSystem game object where the EventSystem is responsible for UI events such as clicking on a button.
In the Canvas game object, I attach the UiManager
script I created and stored in the scripts
folder:
Here I attach the Game Over Text and Restart Text components from the scene onto the UiManager
script:
I make sure to disable the game objects above so the Game over texts don’t show up when the game first plays.
Displaying Game Over Logic
In order to enable the game over screen text to display:
- The player must have all of their lives depleted to 0
- Once that is done, the game over text will display
- The game over text should be blinking every half a second
Some questions I asked were:
- How do I define the functionality to enable the game over text?
- Where should the functionality to enable the game over text be called from?
Game Over Functionality
In UiManager
script I defined the following functions:
- public void ToggleGameOver(bool toggle)
- private IEnumerator FlickerGameOverText()
Calling ToggleGameOver(bool toggle) in the Player Script
In the damage function of the Player
script, I check if the player’s lives reaches 0 or less and call ToggleGameOver(true)
function:
In the next article, I’ll be going over how to allow the player to replay the game. Stay tuned!
Thanks for reading :)