Retro Game Over Behavior
In this article we are going to make the words “Game Over” appear on screen when the play runs out of lives.
First, we need to create a text object in our canvas. In our Hierarchy tab, right click on canvas, go down to UI, and select TextMeshPro.
Name the object Game_Over_Text.
We can adjust the Text, Font, Size, color and a host of other text related elements from the TextMeshPro component on the Game_Over_Text object.
NOTE: You may find that when you increase the size of your text, it disappears from the screen. In your TextMeshPro component, find the setting for overflow.
Set Overflow to overflow. Your text should reappear on screen.
Alternatively, you can manually increase the size of the text box in the Scene view until the text appears.
Since we don’t want the text to appear before the game ends, let’s start by going to our canvas and turning it off.
Select the game over text object in the Hierarchy. In the in the Inspector tab, uncheck the box next to the name. This will make the object inactive.
Next, we need to create logic that says “when the player is out of lives, end the game and display the game over text”.
Let’s start in our UI manager script. For our UI manager to activate the game over text, it has to know that the text exists. For that we’ll create the handle for our game over text object.
If we serialize the field, we can add the reference in the inspector. With our canvas selected, drag the game over text object into the corresponding variable reference box. That will allow us to control the entire text object.
Back to the UI manager script. We use the ‘.SetActive()’ function to turn our text object on and off. This function takes inputs of true and false. they correspond with the checkbox that we unchecked earlier to hide the game over text; true representing checked and false representing unchecked.
In the start method we will set the game over text object to false. this way when the game starts, the text will not appear on screen.
Next, we need to figure out when we turn the text on. That happens when we update our player lives to zero. In our UI manager, under the update lives method, lets create that logic. If player lives is zero, then we want to set the game over text object to active. that will look like this…
Save your scripts, hop into Unity and run the game. If we lose all of our lives, we will see the text appear.
Next, let’s add a flicker to the text, to give it that retro arcade feel.
This will be a behavior that happens when certain conditions are met. How do we accomplish that? Well… The first thing that comes to mind is a Coroutine and a while loop. As long as the game is over, the text needs to flash on the screen.
In the above coroutine we can see the while loop. We’ll set the game over text to “GAME OVER”. Then we will use the yield event to wait 0.5 seconds. We will then set the text to be nothing, denoted by the open and closed parentheses with no string in between them (“ ”). We use another yield event to wait another 0.5 seconds before the loop repeats.
The next part is to call the coroutine. We do this in the update lives method when the player lives is zero. We call it inside the if statement that activates the game over text object.
Save your scripts and run the game.