Scripting the beetle count

As you might imagine, the script for counting beetles will be very similar to the method we used for counting cucumbers. This time, we will add a BeetleManager script to our Beetle_Count UI component. Here is the required script:

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BeetleManager : MonoBehaviour {

public int currentBeetleCount;
Text Beetle_Count;
public GameObject[] beetles;


void Awake () {

Beetle_Count = GetComponent<Text> ();
currentBeetleCount = 0;
}


void Update () {

beetles = GameObject.FindGameObjectsWithTag ("Beetle");
Beetle_Count.text = beetles.Length.ToString();
}
}

This code is very similar to our CucumberManager script. The only difference is the GameObject tag and our variable names.

If this script does not work for you, ensure that your beetles are assigned the Beetle tag.

The remaining scripting of our HUD will be accomplished in subsequent chapters. In Chapter 10, Scripting Our Points System, we will write scripts for the points and number of cherries in Cucumber Man's inventory. In Chapter 11,  Scripting Victory and Defeat, we will write the necessary scripts to update the health slider and the number of Cucumber Man lives that remain.

Be sure to save your Unity scene and project!
..................Content has been hidden....................

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