- Home /
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. :P
Answer by Oribow · Jul 03, 2013 at 06:38 PM
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 :)
What is the: "g" suppose to mean I keep getting errors:
$$anonymous$$ identifier: 'g'.
Thanks though :D
Sorry i write more Code in C# as in js so i forgot the var.
Then you should look at (and upvote/accept) my answer, below :D Edit: Lol just realized who wrote what. Whoops.
Well i have another problem i don't seem to understand. I'm getting the error `BCE0019: 'isBuild' is not a member of 'UnityEngine.Component'. it says it's on the line: "if (g.GetComponent("here the name of the Script").isBuild);"
Answer by tigertrussell · Jul 03, 2013 at 06:31 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613105426im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Question about variabl lists 1 Answer
Find a GameObject without a reference 2 Answers
How do I save an array of all different variables? 0 Answers