|
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?
(comments are locked)
|
|
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. C# example: Is it possible you could post a C# version of this? I'm having a hard time converting this to C#, as I'm a complete newbie.
Dec 20 '09 at 05:26 AM
Polatrite
Added a C# example.
Dec 21 '09 at 12:22 PM
TowerOfBricks
This deserves a -1 for clearly untested code. If you had tested it, you would have realized that you spelled 'length' wrong.
Jul 04 '11 at 06:18 PM
ironmagma
And the first line of the C# example needs to be: Gate[] myGates = FindObjectsOfType (typeof(Gate)) as Gate[]; See http://unity3d.com/support/documentation/ScriptReference/Object.FindObjectsOfType.html But the answer helped me, so thanks.
Feb 29 '12 at 12:10 AM
DanW
(comments are locked)
|
|
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 ...
(comments are locked)
|
