This recipe teaches us how to use multiple Image (Script) components inside a script in order to create a symbolic lives counter. The number of lives are not displayed as a number, but with heart symbols on the screen. It is similar to the counter in the Implementing a lives counter recipe, but the logic to manage different icons is different.
using UnityEngine.UI;
statement at the beginning of the script it is not needed since we will not use any UI classes.private
and one public
, to set this last one in the Inspector so that we can write:public GameObject[] hearts; private int lives;
Start()
function, we need to set up the lives value, so:void Start () { lives = hearts.Length; }
addLife()
, and also here there is a return value to understand if the life is added or not. The function is as follows:public booladdLife(){ if (lives <hearts.Length) { lives++; updateSymbolicLivesCounter(); return true; } return false; }
loseLife()
function, again with a return value to check whether the player has finished his life:public boolloseLife(){ lives--; if (lives > 0) { updateSymbolicLivesCounter(); return false; } lives = 0; updateSymbolicLivesCounter(); return true; }
updateSymbolicLivesCounter()
function. Here the logic is different from the Implementing a lives counter recipe. According to the number of lives, we have to either enable or disable the hearts. The following is the function to achieve this:private void updateSymbolicLivesCounter () { for (int i=0; i<hearts.Length; i++) { if(i<lives){ hearts[i].SetActive(true); }else{ hearts[i].SetActive(false); } } }
We have created several hearts icons inside a GameObject named Symbolic Lives Counter. Finally, we have attached our script to it.
In our script, we have two variables. The first one is an array of game object, called hearts, and as the name suggests us, it will store all the hearts contained in the Symbolic Lives Counter. The second one is the number of lives possessed by the player.
This time, we didn't have a variable that stores the maximum number of lives allowed. So, in the Start()
function, we set the lives
variable to be equal to the length of the array. Since we will set it in the Inspector and it contains all the hearts in the scene, its length represents the maximum number of lives allowed.
Furthermore, we have also written a function called addLife()
that adds lives to the player that returns back a Boolean value: true
if a life is added, otherwise false
if the number of lives is equal to the maximum number of lives allowed and the function. It is quite similar to its homonymous function in implementing a lives counter. In fact, at the beginning, there is an if
statement that checks whether lives are less than the hearts.Length, which is the maximum number of lives. If so, we increase the lives by one, lives++;
, then we call updateSymbolicLivesCounter()
function to update the interface, and, finally, we return true
, otherwise we just return false
.
Now, we also have a function for loseLife()
.This function returns back a Boolean value: true
if the player has no more lives, otherwise false
. First, we decrease the number of lives by one, lives--;
, then we check to see whether lives are more than zero, and, if so, we call the updateSymbolicLivesCounter()
function to update the interface and finally, we return false
. Otherwise, we set lives = 0;
since we don't want a negative number of lives, and, after updateSymbolicLivesCounter()
, we return true
.
In the updateSymbolicLivesCounter()
function, we have to set active or no each heart. In order to do so, we have a for-cycle for every entries of the hearts array. Then, there is an if
statement that checks if the number of lives is more than the heart of that iteration. For example, if the player has just three lives left, we only want to display the first three hearts on screen and not the last two. Therefore, we enable only the first three hearts, and if there are no hearts in the array, we disable them.
In the last step, we assigned each heart objects to the entries of our hearts array.
3.138.174.95