Game Over Dude: Flickering UI text in Unity
Objective: Create game over text which appears and flickers when player’s lives are zero, or less.
Add a UI text element, which will be added as a child of the Canvas. Use the Rect Transform component to center the text in the canvas with X =0, Y=0 and Z=0 coordinates. In the Text component set the font size to 50, color to white allow both horizontal and vertical overflow to overflow using drop down menu. Once we like the look, make sure the check box next to the name, Game_Over_text, is unchecked so it is not displayed at start of the game.
Now enable the text when the player dies, by adding the code below to the UIManager script, which is attached to the Canvas. While there other ways to activate the Game_Over_text, using the transform.GetChild seems efficient. If the lives of the player is zero or less, start the coroutine GameOverFlicker(), which sets the child 2 game object to be active and the after 0.5 seconds turns it off, and again waits 0.5 seconds. This is in an infinite while loop so the process repeats.
The Game_Over_text must be child 2, i.e. the third child in the hierarchy. Now this has some limitations, or features, depending on perspective. The code above flickers child(2) so if the child order changes as shown below, so does the UI element which flickers. Notice, how the flicker can be moved from the game over text to the lives image and then the score text by changing the order of UI elements within the Canvas.