- Home /
All instance of a script and change a variable
Hi Guys
I have a prefab and 5 or 6 of its instance on the scene. There is a move script attached to the prefab with a variable. How can I accesse to every single instance of the game object and change the variable in the same time? GameObject.Find/FindWithTag seems to find only one of them but not all with the same name. If you have any idea can you help me in Javascript please? Thanks Jozsef
you can use var example = GameObject.FindGameObjectsWithTag("object"); and what it does is search for all the objects that have that tag and you access to this using example[0].blablabla
Yeah thanks I have just found the same solution you are talking about on a different forum. I will try it. thanks for the reply.
Answer by Richard J. Hansen · Apr 28, 2011 at 04:04 PM
You could take advantage of sending messages, just give all the prefabs some function to change the variable. However that can be slow if you are doing it alot.
If you are instatiating the prefabs at run time, simply store them in an array. You can then loop through the array changing all the variables.
Your answer
Follow this Question
Related Questions
find object in game view 1 Answer
How could I get a selection set of GameObject sharing a material or a script in Editor? 1 Answer
End game then an Object is close to another object? 0 Answers
Sound when see gamect? need help 2 Answers
How to deactivate objects that are children all at once? 0 Answers