Creating Basic Game Over Behaviour

Gert Coppens
Aug 23, 2021 · 3 min read
Game Over

To demonstrate how you can easily start using the Image UI Element through code, in this article we’ll be looking at creating a basic Game Over system.

The idea is very simple, by code we declare that the ‘Game Over’ Image is turned off by default and if needed, we turn it on. You can apply this to any image for your own project.

In the UIManager.cs Script, create a new private member variable allowing us to reference the Image Element through code, just like we did for the Text Element.

Assign your ‘Game Over’ Image in the Inspector

Now let’s have a look at this function;

In the Start() Method we can use it to disable the Image at first by saying:

Then we could create a new Method that we can access later, to enable the Image:

Enabling the ‘Game Over’ Behaviour

Moving to the Player.cs Script, first thing we do is create a reference to the UI Manager (if you haven’t already), this is needed to communicate through scripts:

Now in the TakeDamage(int damage) method, we can tell the UI Manager the Game is Over when the Player dies.

Great, the basic ‘Game Over’ Behaviour is now functional. Except let’s think a step ahead; knowing that we’ll be upgrading and adding more features to the UI functionality later, we could attach it to a new parent called the ‘Game Over Window’, and turn that on and off instead:

In the following article we’ll continue to improve the UI and introduce a Health Bar for the Player.

Previous | Next

Nerd For Tech

From Confusion to Clarification

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

Gert Coppens

Written by

Software Engineer — Unity Game and Application Developer

Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.