2D Mobile Game: Player Health System


In this article, we will cover how to update the player lives count using the UI Manager, script communications, and some methods.

Player Script

The Player has interface called “IDamageable” that establishes this game object can take damage. The requirements that need to be fulfilled for the interface is getting and setting a Health amount.

In the Start, we set the Health int to 4 since the UI health holds 4 health bars.

Every time the Player gets hit, a Damage method is call which is another requirement for the IDamageable interface.

When this method is called, we subtract one from the current value of Health and pass the in the current Health count accessing the UI Manager instance and calling the UpdateLives method from it.

UI Manager Script

We will dive into the UI Manager script, which is in charge of everything UI related. The first thing we will need to do is create the UpdateLives method we are calling from the Death method on the Player’s script.

This method will have a int parameter which will take in the Player’s current lives. How this method will work is it will loop through each of the health bar images and if the health bar is equal to the lives Remaining parameter in, we can simply hide that health bar.

We need to access the health bar images, so we create an empty Image array in the UI Manager script.

In the inspector, we assign the health bars to the array and making sure the health bars are in sequential order.

The array order

Health Bar 4 = Element 3

Health Bar 3 = Element 2

Health Bar 2 = Element 1

Health Bar 1 = Element 0

It is important to know this order, since we will be looping through each of these elements and turn off the correct one.

Back in the UpdateLives method, we are going to use a For Loop that will increment through each of the lives remaining. If the index equals the lives remaining, we access the health bar at the index value and disable the health bar image.

At this point, we have the Health Bar updating properly and now just need to have the player die when out of health.

Player Death

In the Player Animation script, we create a Death method that will trigger the Death animation for the Player

Player Animation script

In the Player script, every time the Damage method is called we will check to see if the Health has gone below 1. If that is true, we call the Player death animation from the Player Animation script.

The death animation will replay each time the Player is hit after they are out of health.

To fix this, when the Health goes below 1 we can simply return which prevents the code from going any further. This prevents the death animation being replayed.

Another potential issue is make sure the Death animation is not looping.

Until next time…Cheers!



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