In the last article, we created the score and lives UI elements for the Player. This article will make a game over UI element when the Player dies and flicker like in a retro arcade game.
The first thing to do is create a Text UI element and name it “Game Over Text”, then change the Text to “GAME OVER” and adjust the size and position till you get it where you want.
In the UIManager script, create a private Text variable named “_gameOverText”, this will be how we control the Game Over Text. In Start, set the _gameOverText gameobject to false; this way, it will not be visible until we activate it later. Next, in the UpdateLivesImage method, make an if statement that checks if lives are equal to 0 and, if so, set the _gameOverText gameobject to true.
In Unity, hook up the Game Over Text gameobject to the Game Over Text field on the Canvas.
Now in Play Mode, when the Player dies, GAME OVER appears. It just needs to flicker now.
In the UIManager script, create a Coroutine named “GameOverFlickerRoutine”. Inside it, make a while loop that is always true. In the while loop, set _gameOverText gameobject to true, yield for 0.5 seconds, then set the _gameOverText to false, and yield for another 0.5 seconds. Finally, start the Coroutine in UpdateLivesImage, inside the if statement checking if lives are equal to 0.
Another way to get the flicker effect would be to enable and disable the Text component on _gameOverText.
You also could get the flicker effect by changing the text from “GAME OVER” to “”.
Whatever way you choose to achieve the flicker effect, in Play Mode, you can see that the Game Over Text appears and flickers when the Player dies.
In the following article, we will look into making a Menu system and restart the game after the Player has died.