My journey becoming a Unity game developer: Creating a Retro Game Over Behavior.
Objective: Creating “GAME OVER” text to be shown onscreen after the Player dies using Unity’s TextMeshPro UI system. Also, make the “GAME OVER” text flicker on and off while it’s onscreen to give it the behavior of old school video game technology used.
Create a new text game object inside the Canvas by right-clicking on it and selecting UI->Text-TextMeshPro. Rename the text object Game_Over_text, and position it in the center of the screen. In the text field, type “GAME OVER” to show onscreen. Adjust the Font Size to the size you want the characters to show, and adjust the Text field to make the characters fit inside it.
You can also use the Vertex Color or turn on the Color Gradient to set the colors of the “GAME OVER” text. To change the spacing between the characters or words use the Spacing Options setting. Use the Alignment setting to adjust the text inside the text field’s box. If you’re using imported fonts, use the Shader component’s Face and Outline settings to adjust the Color, Softness, Dilation, and Thickness of the text.
Inside the UI Manager script, create a new variable with a SerializeField Type TMP_Text name _gameOverText which will access the Game_Over_text game object. After saving, go to the Canvas object and drag the Game_Over_text object into the Game Over Text variable’s slot.
The “GAME OVER” text appears onscreen after the player loses all their lives. However, the text looks plain and uninteresting onscreen, therefore let’s add a little behavior to it by making it flicker on and off like a light bulb that’s about to go out. This style was popular with older video games back in the late 70’s and 80’s.
Back in the UI Manager script, create a new coroutine name GameOverFlickerRoutine(). With the code block we want to check that WHILE game over is TRUE, get the _gameOverText object’s text field and have it equal to “GAME OVER” text to be seen onscreen. Then YIELD RETURN and wait for 0.5 seconds before going back to the _gameOverText object’s text field and setting it to an empty text field using an “ “ empty quotations to show a blank area in the text field. Last, YIELD RETURN and wait another 0.5 seconds to finish the order of execution inside this coroutine.
Inside the UpdateLives() method, use the StartCoroutine() function under the _gameOverText SetActive() line, and pass in the parameter of the GameOverFlickerRoutine() coroutine. This will now have the “GAME OVER” text flicker over and over as long as the Player has lost all their lives with the “GAME OVER” text active onscreen until a new game is started.
When the Player loses all their lives, the “GAME OVER” text appears onscreen flickering off and on until the game is stopped.