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 ...

Get Getting Started with Unity 2018 - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.