Creating a Retro Game Over Behavior
Objective: Create the Game Over feature to the game.
Following on with the UI from the last post, add a UI text element to the Canvas and rename it Game_Over_text. Change the text tp GAME OVER and increase the font size (40 in this case). You’ll notice it doesn’t show properly so, change the horizontal and vertical overflow settings to overflow, then center both horizontally and vertically.
Now to get it working, start by turning it off in the inspector. Next go to the UIManager script and create a serialized private Text called gameOverText and assign it in the inspector. As a precaution, in the UIManagers start function, set the gameOverText.gameObject.SetActive to false.
Then inside the UpdateLives method, run an if statement to check if lives are equal to 0 and then set the gameOverText to active.
Now as nice as a clean game over text looks, it will look better if it has a flickering effect applied. For this, we’ll use an IEnumerator(Coroutine). Create the IEnumerator called GameOverFlicker() and use a while true loop to keep it running. Set the gameOverText to active then set a yield return of 0.25 seconds, followed by setting the gameOverText active to false and then yield returning after another 0.25 seconds. Finally, remove the gameOverText line from the if statement in the UpdateLives method and start the coroutine instead.
In the next post, we’ll look at unity scenes.