- Home /
Cycle Through All GameObjects in the Scene
There will probably be a really simple answer to this, but what is the best way to cycle through all of the GameObjects in the current scene? With a for loop, maybe? But then how would you write that?
Answer by efge · Mar 26, 2011 at 04:46 PM
To find all objects in the scene with the tag "Enemy" you could use this:
var enemies : GameObject[] = GameObject.FindGameObjectsWithTag ("Enemy") as GameObject[];
for (var enemy : GameObject in enemies) { // do stuff here }
(For performance reasons it is recommended to not use the function GameObject.Find.)
efge, can you explain "as GameObject" after ("Enemy")? I've seen this syntax but don't fully understand it. If you left it off, seems like the array would still populate fine. I assume that stating the type improves performance. Have I got it right?
Answer by gooncorp · Jul 03, 2013 at 12:56 AM
i think he was asking how to find the gameobjects in the scene, not the gameobjects in the scene with a certain tag.