Creating Basic Game Over Behaviour
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.
Now let’s have a look at this function;
- Value: Activate or deactivate the object, where
trueactivates the GameObject and
falsedeactivates 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: