x


How do I iterate through and identify all scene objects?

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?

more ▼

asked Dec 19 '09 at 06:09 AM

Polatrite gravatar image

Polatrite
39 5 5 14

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

var MyGates : Gate[] = FindObjectsOfType (Gate);
for (i=0;i<MyGates.lenght;i++) {
    MyGates[i].OpenGate ();
}

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:

Gate[] myGates  = FindObjectsOfType (typeof(Gate));
for (int i=0;i<myGates.Length;i++) {
    myGates[i].OpenGate ();
}
more ▼

answered Dec 19 '09 at 09:03 AM

TowerOfBricks gravatar image

TowerOfBricks
3.2k 17 25 50

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)
10|3000 characters needed characters left

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 ...

more ▼

answered Dec 19 '09 at 04:58 PM

Ashkan_gc gravatar image

Ashkan_gc
9.1k 33 56 117

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5089
x2090

asked: Dec 19 '09 at 06:09 AM

Seen: 5906 times

Last Updated: Feb 29 '12 at 12:10 AM