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

Javascript is NOT Java!!!!! Spread the word!

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

(sorry for 2 comments) http://unity3d.com/support/documentation/ScriptReference/GameObject.FindGameObjectsWithTag.html

1 Answer

1

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.

Woops, you're right ! I edited that mistake. Thanks !