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?
2 Answers
2To 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.)
i think he was asking how to find the gameobjects in the scene, not the gameobjects in the scene with a certain tag.
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?
– tool55