Now that we know how to build UI elements, we can explore creating a Game Over screen. First, we will need a new text UI element.
We make this these same way we made it for the scoring system.
Let’s name this “Game_Over_Text”. Don’t forget to change the text it displays in the Inspector as well. Mine reads “GAME OVER” and I changed the text color to red.
Position this object where you want it to display on your Scene window. Mine will be centered and slightly higher than the middle of the screen.
Now we don’t want to see this object all the time, only when the player is out of lives.
First thing is to create a new variable on the Canvas game object to link this Game_Over_text to (just like the Score_text).
Drag it onto the component in the inspector and let’s dive into the logic needed to display this.
How does the player lose in this game? They lose their three lives. So the defining variable on whether we see the Game Over screen is based on our lives system.
So if our current lives = 0, display the Game_Over_Text.
Let’s make a coroutine inside the UI Manager called GameOverSequence to handle this game over behavior.
Now we make a function for Update Lives in our UI Manager and use an “IF” statement.
Finally, let’s make sure that the Game Over text is automatically hidden before the game starts up. We can use a SetActive(false) command in void Start.
Now when the player runs out of lives, it should display the Game Over text.
Let’s take it a step further, and make it flicker like the old arcade games.
A flicker effect is just turning the UI element on and off on a timer. We will need to make a coroutine for this in our UI Manager.
This coroutine simply inputs the logic for the Game_Over_Text object to disable and re-enable every half second.
Now we need to call the coroutine when the game over sequence happens.
Let’s see what it looks like!