FindWithTag.transform works, FindWithTags.tranform doesn't. Why?

So why can one use Blah = GameObject.FindWithTag("Blah").transform, but you can't use Blah = GameObject.FindGameObjectsWithTag("Blah").transform?

What's the difference, and is there a simple workaround for finding the transforms of multiple objects with the same tag?

Thanks

yes by using an array []

–

3 Answers

3
GameObject[] blahObjects = GameObject.FindGameObjectsWithTag("Blah");
foreach(GameObject blah in blahObjects)
{
 blah.transform ...
}

find objects returns an array not a single object to use.

Thank you, appreciate it ;)

–

You can use Array.ConvertAll()

GameObject[] Blahs = GameObject.FindGameObjectsWithTag("SomeTag");
Transform[] BlahTransforms = Array.ConvertAll<GameObject, Transform>(Blahs, x => x.transform );

I don't think you can do this? GameObject can't just be cast to Transform, you need to get the transform member of it.

–

That's what I'm doing. The converter inputs a GameObject and returns the attached Transform component.

–

Oh, my bad - my browser was cutting off the rightmost part of your code, so I didn't see the second argument to Array.ConvertAll(). :-)

–

Also LINQ (if you aren't targeting mobile):

var transforms = GameObject.FindGameObjectsWithTag("Blah").Select(g => g.transform);