Find array of a member of GameObjects

I have one problem: I tried a lot. But, could not get throgh :frowning: need some help !

Each gameObject has a bool variable. I need to find an array of such bool variable defined in all GameObject’ in the scene. If one of the bool variable of a given gameObject is false, game is lost.
So, I check in a different script for the occurrence of ‘false’ from each gameObject in the scene.
I’m using GetComponent. but, its not giving me the desired result.
Can somebody please tell me if I’m missing any elementary here ?

void Update(){

GameObject[ ] ActiveCellsArray = GameObject.FindGameObjectsWithTag(“ActiveCells”);

for(int k=0; k< ActiveCellsArray.Length; k++) {
if(!(bool)ActiveCellsArray[k].GetComponent(bool))
{
Result = false;
}
}

}

You are fundamentally misunderstanding a few things:

A GameObject is an actual Unity defined sealed class - we don’t really add variables directly to a GameObject, we define MonoBehaviour derived components with our variables and methods that we then add to a GameObject via the Inspector. For example:

using UnityEngine;

public class MyScript : MonoBehaviour
{
    public bool MyBoolVariable;
}

The GetComponent method returns the MonoBehaviour derived component of the Type that we pass into it - passing in bool makes no sense and won’t even compile. Given the above MyScript definition, you want to do something like this:

for(int k = 0; k < ActiveCellsArray.Length; k++)
{
    MyScript myScript = ActiveCellsArray[k].GetComponent<MyScript>();    
    if(myScript != null  !myScript.MyBoolVariable)
    {
        Result = false;
    }
}

Substituting with your own types and variable names.

Cool?

Awesome post. Thanks a lot for throwing some light on GameObject.
And, finally: your code snippet worked like a mana for my HP :slight_smile: