Creating a Retro Game Over Behavior

Lets Make it Flicker Too.

Now that we have run out of lives, it is time we tell the player the Game is Over.

Game Over

In order to add this text to our User Interface, we will need to add a text object.

Select Canvas, right click and select UI, Text.

Creating the Text object in Canvas

Lets rename our object. I will call it Game_Over_Text. In the Text window, change the text to Game Over. Lets set the text color to white.

Formatting Text object

The text looks rather small on the screen. If we try to increase the text size by changing the font size, the text seems to disappear. To avoid this, we need to set both the Horizontal and Vertical Overflow options to Overflow.

Adjusting Horizontal and Vertical Overflow

Now we can resize our text, center it and set the bounding box to fit. Lets set the font size to 50.

Resizing and cetering text object

No we need to make our Game Over text appear when the player has lost all his lives.

This will be done in the UIManager script.

We first need to create a variable of type Text so we can connect the text object to the UIManager script.

New Text Variable

Back in Unity, select the Canvas object and there should be a new Game Over Text inside the UI Manager script. Drag the Game_Over_Text into this slot.

Connecting the Game_Over_Text object to the UIManager

Now, where would be the best place to activate this object when the player loses all their lives?

There is a UpdateLives method in the UIManager script. This is where the lives sprites are being updated. This is the perfect place to check if the player is still alive and activate the text object if not.

Activating Text object if player lives = 0

The Game Over text should now display when the player’s lives reaches 0.

Game Over working

We need to make sure this object is not activated when we start our game. To do this, in the Start method in UIManager script, set the object to false.

Turning text off at start

Although the Game Over is functional, it is very static. Remember back in the day, when we lost, the Game Over would flash on the screen until we put in another quarter.

Lets make our Game Over flash.

For this we will need to create a coroutine in the UIManager script. There are a couple of ways to do this:

SetActive to true and false

By setting the text gameobject’s SetActive to true and then false with a 0.5 second interval, it will give the sensation of a flashing text.

Adding and deleting the text from the object

By setting the text in the object and then removing it, we can also create the sensation of a flickering text.

Now that we have our coroutine setup, we need to call it. So, from the UpdateLives method, in the if statement, we call the start the coroutine.

Starting the coroutine

Wait a second, that didn’t flash the text! The reason is we need for this coroutine to loop in order for it to create the flashing sensation.

In the coroutine, lets wrap our script in a while(true) loop.

while(true) loop

This will make the script run over and over in an endless loop.

Flashing Game Over text

There, that looks a lot better!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Marc Frappier

Marc Frappier

Poultry Scientist by profession, Computer Scientist at heart. After many years in the poultry business, I have decided to give my passion for technology a shot.