Unity Development — Retro Game Over
It is inevitable that at some point you will players lose in your game. If you designed a game that everyone beat without any challenge, it wouldn’t be very enticing. Your game must know how to handle the eventual defeat of the user. There are endless ways to do this, but for simplicity sake, we are going for an old school method.
The player’s health is down to zero, or they’ve lost all their lives, or time ran out. Whatever it is, the game is over. What easier way to convey this than to simply tell the player. Make a UI text in the Hierarchy and have it say ‘Game Over.’ You can even use all capitals or tint it red if you are feeling extra dramatic.
Once that is made, go to the ‘Text’ component on the screen and disable it. The way to do that is to unclick the little box next to the component name. Once that is done, the text will disappear from the screen. Time to get into some code.
Go to whatever script it makes the most sense to place your game over logic. This could be a Game Manager or UI Manager. It is common practice to have both and for the UI Manager to be placed on the Canvas itself, as it holds all the UI anyways.
Let’s say we are using a UI Manager. At the top declare a private serialized Text and call it ‘gameOver_text’ or something along those lines. This may give you an error. In order to use UI in your scripts, you must add the namespace. Where you see the other namespaces at the very top, add ‘Using UnityEngine.UI;’ When this is done, take your Game Over text from the Hierarchy and drag it in to the Inspector to fill that variable.
Now we are going to get to the real retro part. Make a public void method that will be called when the Game Over conditions are met. From there we are going to call a coroutine. Coroutines are used to wait for a certain amount of time and that is exactly what we are going to do.
Create your coroutine and in it use YourVariableName.enabled = true. Then use the coroutine ‘yield return new WaitForSeconds() to wait a half second or whatever you like. Repeat the same thing, only setting enabled to false. Put this all in a while(true) loop, so that it happens infinitely. Now watch what happens at a game over.
Now THAT is some old school blinking. It is simple, but still effective.