Nerd For Tech
Published in

Nerd For Tech

Creating a retro-style Game Over screen in Unity and C#

Game Over bro!

One of the main challenges with the any game is to progress as far as possible — what makes this a challenge? The chance to not progress further of course…

Introducing a gaming staple — the Game Over screen! Following on from my last article on UI building, we need to be able to notify the Player when they fail and lose all of their lives.

As with before, I will be using the TextMeshPro component for this (although, using the correct namespace libraries, the the coding solution would work just as well with an ordinary Text component too).

With the GameObject active and the TMP component inactive, we can start the game with our Game Over message hidden from our Player.

This means that obviously, we will need to activate the message when the Player reaches the Game Over conditions — i.e. has run out of lives.

Enabling Game Over message
Checking lives in the Player script

Very simply, in our UIManager, we can grab a reference to our Game Over Text component and enable it to show it to the Player when called upon.

Adding that call after a check to see if our Player has any lives left will then display the Game Over message at the appropriate time.

Just pinging up a Game Over seems like a bit of a boring Game Over message though, so let’s spruce it up a bit.

NOTE : How this screen will look is entirely up to your own imagination and creativity. The following will demonstrate the methods used to create the example at the top of this article.

Declaring required variables

Firstly, our variables — there will be a lot of experimentation as you attempt to get the desired effect for your Game Over. Having the variables assignable in the inspector allows us to quickly reiterate the effect with little tweaks as we fine tune the effect without having to keep going back to our code.

There are also a few lines added to our DisplayGameOver method —

string msg = _gameOverText.text;

_gameOverText.text = null;

StartCoroutine(GameOverRoutine(msg));

By assigning the value of our Text component to a variable, msg, and then ‘nulling’ or clearing out it’s contents, we keep a reference to what that value was — our Game Over message — and it appears to the Player as a blank message to begin with.

Then, as with many timed effects you may want to create, we trigger a Co-routine while passing our msg string into it —

Effect Co-routine

And in this Co-routine is where the effect is governed.

WaitForSeconds letterDelay = new WaitForSeconds(_letterDisplayDelay);

WaitForSeconds flashDelay = new WaitForSeconds(_flashDelay);

With this code, we are setting our variables _letterDisplayDelay and _flashDelay as the yield delays we will use.

The ‘for’ loop then iterates through our string variable’s length, adding one character to our Game Over Text each time. With the delay in the loop, it gives the effect of spelling out our msg one character at a time.

The alignment setting of our text component is what sets the appearance of our continually growing message. As I have it set to centered, the message grows from the middle of the Text component.

Once our message has been spelt out, our code then drops into a ‘while’ loop that turns our Text component on and off, giving it the blinking effect, this continues up to the number of times specified in our _flashCount variable.

As you can see, this is a very simple yet effective addition to our game. Have a play with the Text component settings and delay timers to achieve the look you want for your game.

--

--

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
Les Street

Les Street

27 Followers

A UK based Unity Developer with a passion for gaming and coding