Adding Game Over UI elements
At the moment, when our player loses all 3 lives, nothing happens.
The game keeps running but the player can’t interact.
We should fix that!
Add Game_Over_Text game object for our UI as shown above.
Toggle off the active status for our new text field, leave it anchored to the center, and choose middle alignment for both horizontal and vertical placement.
In our UIManager script, let’s add the variable where we can store this text field as well as the associated game object.
Drag and drop assign our Game_Over_text game object onto our UIManager script component on our Canvas game object.
Back in the UIManager script, we update our Start() method to assign the gameobject of Game_Over_text with a null check for _gameOverText.
Now we add the public ShowGameOver() method so that our player script can call this method when 0 lives is reached.
And lastly we call our UIManagerScript.ShowGameOver() method from the LoseLife() method in our Player script, wrapped in a null check for UIManagerScript.
We now have the basic display Game Over text functionality in working order.
Let’s make our Game Over text flicker in classic arcade style.
First, let’s add a new Boolean variable to check the game over status for the while loop we will be using in our coroutine.
Next, we’ll add the FlickerGameOverTextRoutine() coroutine to our UIManager script.
Then we need to update our ShowGameOver() method to set _gameOverStatus to true and call our new coroutine.
If we run the game, our flicker effect should be working.
Adding Game Over Restart Level text
We’ll create a new text game object on our Canvas, Restart_text and edit the text to instruct the player on how to restart.
The font size should be set smaller than Game Over, I’ve chosen 16.
And once again we change our alignment settings to middle horizontal, middle vertical.
Toggle the active status of the Game_Over_text to on and then reposition the new Restart_text below it.
For both Game_Over_text and Restart_text, in the Unity Editor, default their active status to false by unchecking them as shown above.
In the UIManager script, we add some variables for our new Restart text.
In our UIManager script we update our Start() method to assign the _restartGO game object from the _restartGameText variable.
I don’t want this line of text to flicker, so we simply add a SetActive statement to our ShowGameOver() method and we should be in working condition.
In the next article we will implement the game restart code.