Creating a Retro “Game Over” Behavior.
Objective: Via the Unity UI, project a flashing “Game Over” message.
What better way to inform the player that his quest for victory has been dashed by flashing an ominous “GAME OVER” banner across the game scene! Follow along as I quickly show you how to implement this feature into your own game.
You begin by creating a private text variable in your UI Manager. In my case, I’ve opted to use Text Mesh Pro (TMP_Text) and called it _gameOverText.
Once I have the font type, size, position, color, etc…all set up to my liking, I need to turn off the text. Why? Well, if I don’t, it’ll just confuse the player :).
I do so in the void Start() method by setting the SetActive attribute of my game object (_gameOverText) to “false”. Just like that, it’s “turned off” and remains invisible even though it continues to sit on my canvas.
As the player loses his life in the game, the UI Manager looks at the int value of the currentLives variable and turning on or off the appropriate sprite image corresponding to the status of his remaining lives. Once the currentLives equals zero, it executes the GameOverSequence() method.
This is where we turn back on the hidden TMP_Text fields. We start by telling our Game Manager to run its GameOver() function which basically allows us to navigate the game by looking for specific key inputs from the keyboard. We then turn on the GAME OVER text, as well as the instructions guiding our player on how to restart the game, or how to return to our main menu. The magical retro game look comes from the coroutine GameoverTextFlicker().
The GameOverTextFlicker() coroutine is fairly simple. “While” it is true that the game is over, let's continuously loop this coroutine, turning the _gameOverText off and on at 0.5-second intervals. This sequence will continue until the player either decides to restart the game or return to its Main Menu scene, either of which will interrupt the coroutine by setting the condition that the game is over back to false.
There you have it. A flashing, “in-your-face” game over message bound to engage the player’s competitive spirit to a rematch. Would you like to play again? ;)
Thanks for reading!