Let’s get old school!
Creating a Retro Game Over Behavior
So you’ve been defeated by the alien ships? That means Game Over! What better way to show the player that they lost than a retro style “Game Over” screen? Let’s create it!
For the Game Over screen we actually don’t even need to create a new screen, though we might in future versions or other games. In our case we simply need to create 2 text objects and add them to our canvas. One for our Game Over text, and a second for the instructions to restart the level.
After positioning our text in our scene we deactivate it so that it doesn’t show while we’re playing. Next we need to go write the code to activate it. Since we are dealing with changing the display of our UI items, we are going to place the code into our UIHandler class. We give our UIHandler Serialized private fields to hold the references to our UI elements. We also want to be able to control the flash rate from the designer, so we will add a serialized field for that as well.
Next, in Unity, we drag our Text objects from the hierarchy into our inspector fields on the UI manager script.
Now that we have our text objects, we do some basic null checking to make sure we aren’t trying to modify objects that aren’t there and throw some debug messages if they fields are empty.
Now we can create a method that allows us to set our text objects to active which displays them to our user. This function will also kick off a Coroutine that turns our “Game Over” text on and off every half a second because this kind of flashing text was common in retro game over screens.
All that’s left is for us to trigger the method of our UIManager when our game ends. That happens when our player loses all of their lives. We are already handling this in our game manager where we are checking our lives count and updating our lives UI. I’m going to choose to put our UI manager call here because I am trying to keep calls to my UI manager and Spawn Manager out of the player class and letting the Game Manager class handle them. At least that’s the plan until I can decouple this code in the future.
Now we have a retro Game Over screen! Next time we’ll take a look at how to take that “Press ‘R’ to restart” text by reloading the scene. If you enjoyed this article, or want to come along with me as I progress on my journey, follow me at gamedevchris.medium.com.