
Building UI in Unity
Creating a Score board and Lives Counter (part 3)
Now it is time to link the lives UI to the script and have the script display the correct amount of lives left after being hit by an enemy. To do this, we will be utilizing something called “Arrays” and we will have the script swap out the correct sprite within the array.
How Arrays work?
Arrays are variables that is able to collect and store other types of variables within a list. To use an array, basically type “[]” behind a variable to create the array. Within the inspector you are able to set how many elements the array will have, in player’s life example we shall set the amount of elements to 4.


After you have assigned how many sprites are going to be within the list, we will need to have the UI Manager script update the corresponding element to the player situation. (ie. if the player has only 2 lives left, the script will use Element 2).
Updating the Array Index
Within the UI Manager script, we will be creating a new public void method called “Update Lives” with the integer parameter “current lives”. Then within this method, we will need to firstly display the image sprite (ie. 3 lifes) then swap it within the array index.


Using the image handle we can utilize the “.sprite” we can get or set a function for the handle. In this case we call upon the array and have it access the image within the index.
Finally within the player script, we call upon this method within the Damage method using the UI manager.

