- Home /
GameObject.findGameObjectsWithTag returning empty?
I have one game object in my scene with the tag "Enemy", yet no matter what I try, GameObject.FindGameObjectsWithTag("Enemy") returns an empty array...
The variable I tried setting it to is indeed an array of GameObjects, and I even tried
Debug.Log(GameObject.FindGameObjectsWithTag("Enemy"));
while standing right next to the enemy, and it printed out "UnityEngine.GameObject[]"
Why won't it find my game object with the tag Enemy?
And P.S. please don't say to use FindGameObjectWithTag (SINGLE). There is only one now because it's a work in progress. The script is supposed to find many enemies at once
Answer by vexe · Mar 15, 2014 at 07:50 AM
FindGameObjectsWithTag
returns a GameObject[]
- how do you expect Debug.Log
to understand how to print an array of objects like that?
var enemies = GameObject.FindGameObjectsWithTag("Enemy");
foreach(var e in enemies)
Debug.Log(e.name);
What you're doing is:
Debug.Log(enemies);
Which is equal to:
Debug.Log(enemies.GetType().ToString());
or
foreach(var e in FindGameObjectsWithTag("Enemy"))
Debug.Log(e.name);
Your answer
Follow this Question
Related Questions
How to find one object in specific layer? 1 Answer
Find child of a Game Object using tag 4 Answers
difference between GameObject.FindGameObjectWithTag(Tag tag) and GameObject.FindWithTag(Tag tag) 3 Answers
Array Question Length (C#) 8 Answers
Replace variable with new game object after it is made null - not working 0 Answers