120 Days to Master Unity (Day 7)

Creating a retro game-over behavior

Brian Perry
4 min readMay 10, 2021

Today, I will describe adding a flashing “retro” Game-Over text to the screen as soon as the player looses all of their lives. Because there is already a UI_Manager script in my game and this script is already attached to the Canvas, its not really all that hard to add a game over text to the screen. If you regularly read my articles and/or know a little about Unity, you could possibly realize right away that creating a “Game Over” text can easily be done by creating a component and turning that component on an off. This can be done by using the SetActive() function and setting that function to either true of false. SetActive(true) or SetActive(false). In this case, the idea is to set the text active and inactive over time creating a blinking effect. If you have also been following my series, you would also realize that Coroutine would be the best candidate to make this happen since it could be used to call a function every so often to create a blinking effect. I already have a Coroutine in my SpawnManager script, for example, to regularly tell my Enemy script to keep generating enemies at random positions at the top of the screen playing area until the player is not alive or quits. Coroutine can be used, in this new case, to make a blinking “Game Over” text. It can be set to just keep calling a function to turn a script component on and then later to call the script component to turn a component off. This can be done in a loop until the player eventually restarts the game.

The first thing that needs to be done is to create a text UI text game object. This can be done by clicking on Game-Object in the menu. Select UI and then selecting text. A text UI object should appear on the scene screen in Unity.

Now that the UI text object is created and appears in the hierarchy, attach the UI text element to the canvas game object. If the canvas is already there, the UI text object can be dragged to the canvas object. If this is the first UI object created however, a Canvas element will also be created along with the UI Text object in the hierarchy. If this is the case, it should automatically be attached to the new canvas.

To control the behavior of the blinking “Game Over” scene, there has to be a script attached to the Canvas object. In my case, I have a UI Manager script attached to the canvas to control and/or give behavior to the text or anything else attached to the canvas.

A word about scripts and objects. Scripts and Game objects can get a little confusing. In my example, my UI manager C# script, is a component of the Canvas Game-Object in the hierarchy. Once you write something in the UI manager script then, it affects the canvas Game-Object it is attached to. I know I would get confused giving behavior to anything attached to the canvas because my script that defines the behavior was not named the same as the canvas object. I had to pay special attention to make sure I remembered that the script I created to control the behavior to things attached to the script, had a different name. I also got a lot of errors using GetComponent because of this because I would assume I was getting a component named “UI_Manager”. The last step is to position the UI text object on the screen where you want it to appear, anchor it, and type in the game over text in the text slot.

Now that the “Game Over” Text UI is ready, there needs to be a way to get a reference to the newly create “Game Over” object. This will allow the text to appear when it needs to appear and also to blink until the game is reset. To get a reference to the new UI text object, create a variable in the UI Manager script to hold a reference to the new UI text element in the UI manager script. Make sure to make it a SerializedScript and private variable.

Making the variable Serialized will cause a new slot to be shown. This is where the text UI can be dragged into the UI_Manager script. Drag the UI text object from the hierarchy into the new variable created by the UI manager script.

To the the game over text to appear after the player dies, we can have the Player script to call the UI_Manager script just before the Player object is destroyed. As usual, this is done by creating a UI_Manager object and using GetComponent in Start() to get an instance of UI_Manager. Once this is done, the instance of the UI_Manager can be used to call a method in UI_Manager that displays the text. In my case, my method is named flashGameOver().

To get the game over script flashing, we can use a Coroutine. In my example, I use StartCoroutine(displayGameOver()). This starts the Coroutine. Don’t forget to use IEnumberator for the method return type. Using this allows the keyword yield to be used that will pause the execution for however long is specified. I use a loop to set the text object active or inactive.

--

--