Creating a Retro Game Over Behavior
Lets Make it Flicker Too.
--
Now that we have run out of lives, it is time we tell the player the Game is Over.
In order to add this text to our User Interface, we will need to add a text object.
Select Canvas, right click and select UI, Text.
Lets rename our object. I will call it Game_Over_Text. In the Text window, change the text to Game Over. Lets set the text color to white.
The text looks rather small on the screen. If we try to increase the text size by changing the font size, the text seems to disappear. To avoid this, we need to set both the Horizontal and Vertical Overflow options to Overflow.
Now we can resize our text, center it and set the bounding box to fit. Lets set the font size to 50.
No we need to make our Game Over text appear when the player has lost all his lives.
This will be done in the UIManager script.
We first need to create a variable of type Text so we can connect the text object to the UIManager script.
Back in Unity, select the Canvas object and there should be a new Game Over Text inside the UI Manager script. Drag the Game_Over_Text into this slot.
Now, where would be the best place to activate this object when the player loses all their lives?
There is a UpdateLives method in the UIManager script. This is where the lives sprites are being updated. This is the perfect place to check if the player is still alive and activate the text object if not.
The Game Over text should now display when the player’s lives reaches 0.
We need to make sure this object is not activated when we start our game. To do this, in the Start method in UIManager script, set the object to false.
Although the Game Over is functional, it is very static. Remember back in the day, when we lost, the Game Over would flash on the screen until we put in another quarter.
Lets make our Game Over flash.
For this we will need to create a coroutine in the UIManager script. There are a couple of ways to do this:
By setting the text gameobject’s SetActive to true and then false with a 0.5 second interval, it will give the sensation of a flashing text.
By setting the text in the object and then removing it, we can also create the sensation of a flickering text.
Now that we have our coroutine setup, we need to call it. So, from the UpdateLives method, in the if statement, we call the start the coroutine.
Wait a second, that didn’t flash the text! The reason is we need for this coroutine to loop in order for it to create the flashing sensation.
In the coroutine, lets wrap our script in a while(true) loop.
This will make the script run over and over in an endless loop.
There, that looks a lot better!