A Simple Game-Over Screen in Unity

Liberty Depriest
3 min readJul 9, 2022

--

Goal: Learn how to create a game-over screen in Unity

This one, though I call it simple, has a bit of tricks to navigate around. And of course, that tricky part is the coroutine.

Before getting into the code, however, let’s create the UI element needed for this screen. All it is, is a text object.

Right click in your canvas and go to UI>Text to create one, and name it as GameOverScreen.

Hierarchy view

Then let’s modify the Rect Transform component, in the width and height, to make a desired size. Then in the Text component, modify the text and font size as 50. And of course, align it and change its color.

GameOverScreen
Scene view

By default, we won’t have this screen open, so turn off the object by unchecking the box next to the object’s name.

Then let’s get into the logic of a game over screen.

The “game over” for our Space-Shooter game here, is that once the player dies, then it’s game over. So where we detect if our lives is 0, we call to the UIManager script’s “GameOver” method.

Player script
Finding reference for UIManager in Player script

Now in the UIManager script, which is on our canvas, we have our GameOver method which does two things: enable the gameOver bool to true, and start the GameOverFlicker coroutine.

UIManager

We need this bool in order for our coroutine to work. By calling a coroutine once without updating it, it will only run once. We want the game over screen to be flashing indefinitely. So we have to make a while loop using this bool to keep repeating that flashing behavior.

Notice how this coroutine has two pauses, one after we enable the screen, and one after we disable the screen. This is needed so that we get even .5 second pauses between the two states.

Also, yes, it only is just enabling and disabling the object. We can use the SetActive method and pass in a bool to do this.

And this is what we get:

That’s all! Thanks for reading this short and simple article. Hope it helps and have a great game-dev day!

--

--