When to Stop — A Game Over Screen
Currently when the player dies, the game just keeps running with no new input possible. Setting up a game over screen will add some clarity to the experience.
So first off, when should the Game Over sequence happen?
When lives hits 0 and the player is destroyed!
We can create a new UI text object (see previous articles for more UI information) to pop up when the player dies.
In the UI manager script we need to set this new text object as a variable and then have a function that can activate it when the player dies.
I also created a restart UI element that we will add function to momentarily.
Now, a new function to turn on these UI elements.
Notice the coroutine at the bottom?
I created a coroutine with a while loop to simulate the old arcade “Game Over” screen flashing!
Great, now we still need to actually call the GameOver function on the UI manager. We can do that from the PlayerController C# script!
Now one final piece of logic to add to the UI manager C# script; the ability to reload the level ONLY after we have died.
Lastly, the scene needs to be added to Unity’s build options to be loaded correctly.
Let’s see it in action!