http://answers.unity3d.com/questions/93/how-do-i-iterate-over-all-scene-objects-from-an-editor-script I used the above technique to iterate through my scene objects. However I'm still unsure on how to identify what these scene objects are - for example I have a "gate" prefab. I want to allow a certain keystroke to open all gates, so I loop through all scene objects, identify that they are of the "gate" type, and then I execute the necessary code to open them.
What is the best way to do the iterating and identifying process? This seems like something that it's practically impossible to develop games without, but my searches didn't turn up hardly anything!
Does Unity handle this type of thing differently?
asked Dec 19 '09 at 06:09 AM
You probably have a gate script on each of your gates, then you can search for the Gate script instead of searching for all gameObjects.
This will probably work. If you don't have a script attached to the gates you can do a search for all objects with a certain tag, a "gate" tag which you mark all your gates with.
The Stone's answer is correct. also you can use their name to find them. GameObject.Find (string name) is the function that you can use. this function will return null if find no object. the best approach is the code shown above if you have a special script attached to gates. in most cases you have, i think. in this way you can create reusable codes so don't put gate's special code in a general script for gates and doors and windows and ... create a door component and a gate component and ...
answered Dec 19 '09 at 04:58 PM