Get list of gameObjects and change booleans within them

Hello, Basically what I want to do is get a list of gameObjects that has the tag: “SetObject” and then i want to check a boolean within them called: “isBuild” and if it’s true i want to put it in a array of gameObjects i want to do this with hundreds of objects. Then for each one i want to instantiate a object on top of the objects in the array. I’m guessing it would go something like this:

var gos : gameObject[];
gos = gameObject.findobjectsWithTag("SetObject")
foreach (object in gos){
if (object.containsVariable(isBuild) = true)
instantiate(prefab, Vector3(object.X, object.Y, object.Z), Quaternion.identity);
}

Thanks in advance if you actually know how to do this. :stuck_out_tongue:

Your way of thought is generally correct. To store a variable in a GameObject you have to use a script.

Try this one:

var gos : GameObject[];
    gos = GameObject.FindGameObjectsWithTag("SetObject");
    foreach (g in gos){
    if (g.GetComponent("here the name of the Script").isBuild)
    Instantiate(prefab,g.transform.position, Quaternion.identity) as GameObject;
    }

Please keep in minde: isBuild must be public!

Good Luck :slight_smile:

You cannot arbitrarily add properties or attributes to the GameObject class.

Instead, you’ll need to make a script that contains the information that you’re hoping to store, and attach that script to the GameObjects in question.

For instance, and I apologize, as I use C# and not JS:

GameObject gos[] = GameObject.FindGameObjectsWithTag("SetObject");
    foreach(GameOobject object in gos) {
       //This is my custom component/script
       Storage s = object.GetComponent<Storage>();
       //Make sure we have one and that its set.
       if(s != null && s.isBuild) {
          Instantiate(prefab, object.transform.position, object.transform.rotation);
       }
    }

I hope this answered your question.