Number Count with Array

Hello everyone, I am having a doubt about Array and number, I have a scene where I am stuffing carton boxes into a container which is perfectly fine for me, but I need to add a few details to it, which is add a text which says how many boxes are remaining to load into the container can anyone please help me with this, Thank you in advance and I am adding my code and image below.

using System.Collections;

using System.Collections.Generic;

using UnityEngine;

using UnityEngine.UI;

public class Count : MonoBehaviour

{

   public int remainingBox;

   public bool isMoving;

   public Text displayText;

   public GameObject[] childCount;

  void Start()

  {

   

     childCount = GameObject.FindGameObjectsWithTag("CartonBox");

     

  }

  void OnTriggerStay(Collider other)

  {

    //  if(other.tag == "Door")

     

    //  {

         displayText.text = remainingBox.ToString();

        if (Input.GetMouseButtonDown(0) && !isMoving && remainingBox > 0 )

     {

         

      isMoving = true;

        remainingBox --;

        isMoving = false;

     }

    }

  }

There are so many questions so not sure where to begin:

a) Why do you have childCount (of type GameObject array)… it doesn’t seem to be used in any capacity… also you could improve the naming so your intent is clearer. Why not call it cartonBoxes instead??

b) remainingBox is never initialised, so will always have value of 0… did I miss something? did you forget portion of your code? (maybe it is initialised by code in another class?)

Privacy & Terms