# Count # of things in a box? Bools & Arrays...

I have a box, and each box has a child called a slot.

The size of the box depends on the number of slots.

Each slot knows whether or not it is full, and has a bool value called, “isFull”. If the slot is full, then isFull is also true.

My problem is with the box game object. It knows the number of slots, but i need it to know how many slots in the box are full.
I am trying to GetComponentInChildren to create an array of bools, but i am getting various errors, and i’ve tried a few different things.

Additionally, i want boxCount to increment for each “true” value in the bool array. That way, when the number of full slots = the size of the box; the box will know it’s full.

``````	int boxCount = 0; // Number of things in the box
int boxSize;
bool boxFull = false; // Is the box full?
//int[] slot;
bool[] slotIndex;

// Use this for initialization
void Start () {
boxSize = transform.childCount;
Debug.Log ("Children = " + boxSize);

}

// Update is called once per frame
void Update () {

slotIndex = new bool[]{
GetComponentInChildren<slotBehavior>().isFull == true
};

boxCount = slotIndex.Length;
Debug.Log ("Followers in Box: " + boxCount);

}
}
``````

A few things wrong:

• GetComponentInChildren only returns one component
• slotIndex.Length tells you the number of slots returned, but not whether or not they are full

You probably want something like this, to count totals in a loop:

``````int numFull = 0;
int numTotal = 0;
foreach (slotBehavior slot in GetComponentsInChildren<slotBehavior>()) {
if (slot.isFull) {
numFull += 1;
}
numTotal += 1;
}

if (numFull == numTotal) {
boxFull = true;
}
``````

You could cache that GetComponentsInChildren() call for slightly better performance.

I tried to keep it as close to your original as I could, and made it so it only checks the box’s contents on mouseclick since checking it per frame is pretty hectic. I don’t have a chance to test it, but I’m fairly sure this will work the way you want with some comments to help you understand.

``````    int boxCount; // Number of things in the box
int boxSize;
bool boxFull = false; // Is the box full?
slotBehavior[] boxSlots
bool[] slotIndex;

void Start () {

boxSize = transform.childCount;
Debug.Log ("Children = " + boxSize);

}

void Update () {

if(Input.GetMouseButton(0)) {
boxCount = 0;     //[1]
boxFull = false;
boxSlots = GetComponentsInChildren<slotBehavior>();     //[2]
slotIndex = new bool[boxSize];     //[3]
for(int i = 0;i < boxSize;i++) {
slotIndex _= boxSlots*.isFull;     //[4]*_
``````

_ if(boxSlots*.isFull) boxCount++; //[5]
}*_

* if(boxCount == boxSize) {*
* boxFull = true;*
* Debug.Log (“Box is full”);*
* }*

* Debug.Log ("Followers in Box: " + boxCount);*
* }*

}

//[1] - Every time you recount the slots we’ll need to reset everything

//[2] - GetComponents will return an array of slotBehaviors, which we’ll store

//[3] - This is your array of the slots with bools representing whether they’re full, its size is how many slots you have

//[4] - Iterate through slotIndex and store whether the corresponding slot is full or not

//[5] - If the current slot is full, increase boxCount by one