Nerd For Tech
Published in

Nerd For Tech

Creating Basic Game Over Behaviour

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;

  • Value: Activate or deactivate the object, where true activates the GameObject and false deactivates the GameObject.

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

--

--

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