My 90 day journey becoming a Unity game developer: Day-24
Objective: Adding game over text to the game when the player loses all 3 lives. Plus, make the game over text flicker off and on.
To start, on the Canvas game object right-click and under UI select Text to create simple text. Name Text game object to Game_Over_Text. Then change the font size to 50 or whatever size you prefer. We won’t be able to see the text because it will be too big. So change the horizontal and vertical overflow to Overflow and you should be able to see the text onscreen. Also, change the font color to white which will make the text easily visible onscreen. Inside Text area write Game Over and the words will appear onscreen. To center the text, go to Alignment and select the Center paragraph.
Let’s go to the UIManager script and create a new serialize Text type variable name gameOverText.
Go inside the Start() function where we will set the gameOverText game object to be turned off when the game starts.
On the Canvas, drag the Game Over Text game object to the game over variable under the UI Manager script component.
We will go inside the UpdateLives() method and check if lives equals zero to show the game over text.
Now we will create a coroutine to control how the text will flicker and the time in-between the flickering name GameOverFlickerRoutine(). With this coroutine we want to say while this coroutine is true to show the game over onscreen, then wait a half-second before showing no text onscreen. After waiting another half-second, keep it alternating between showing and not showing the text onscreen.
Inside the UpdateLives() method, when the current lives equals zero the game over text is activated. Right after being activated we want the GameOverFlickerRoutine() to run and make the text show on and off.
Next time we will create a way to restart the level.