Updating the HUD with lives remaining

In the previous section, we modified the CucumberManManager script to track the number of lives our player has remaining and, when none were left, the appropriate animation was played. In this section, we will continue modifying the CucumberManManager script to update the HUD with the number of lives remaining.

We only need to modify the CucumberManManager script's Update() method. The completed Update() method is provided here with an explanation of the changes made:

     void Update () {

if (collectingCherries) {
if (tempCurrentCherryCount ></span>= 60) {
currentCherryCount = currentCherryCount + 1;
tempCurrentCherryCount = 0;

_ptsManager = GameObject.Find
("Score_Value").GetComponent&lt;PointsManager>();
PointsManager.currentScore =
PointsManager.currentScore + 5;

} else {
tempCurrentCherryCount = tempCurrentCherryCount + 1;
}
}

if (livesRemaining == 2) {
Destroy (GameObject.Find ("Life3"));
}
if (livesRemaining == 1) {
Destroy (GameObject.Find ("Life2"));
}
if (livesRemaining == 0) {
Destroy (GameObject.Find ("Life1"));
anim = GetComponent&lt;Animator> ();
anim.Play ("CM_Die");

}
}

We added conditional statements to check for the number of lives remaining. When two are left, we destroy the third life image. We destroy the second one when only one life remains, and destroy the first life image when no life remains. We used the Destroy() method to accomplish this. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset