x


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

more ▼

asked Apr 21 '11 at 10:34 PM

superventure gravatar image

superventure
634 44 53 63

yes by using an array []

Apr 21 '11 at 10:36 PM AngryOldMan
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first
GameObject[] blahObjects = GameObject.FindGameObjectsWithTag("Blah");
foreach(GameObject blah in blahObjects)
{
 blah.transform ...
}

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

more ▼

answered Apr 21 '11 at 10:36 PM

Bryan 4 gravatar image

Bryan 4
454 7 8 19

Thank you, appreciate it ;)

Apr 22 '11 at 01:11 AM superventure
(comments are locked)
10|3000 characters needed characters left

You can use Array.ConvertAll()

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

answered Apr 22 '11 at 12:26 AM

Peter G gravatar image

Peter G
15k 16 44 136

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.

Apr 22 '11 at 09:45 AM superpig

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

Apr 22 '11 at 10:00 AM Peter G

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(). :-)

Apr 22 '11 at 06:43 PM superpig
(comments are locked)
10|3000 characters needed characters left

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

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

answered Apr 22 '11 at 09:44 AM

superpig gravatar image

superpig
557 8 9 24

(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:

x1274
x205
x148
x137
x33

asked: Apr 21 '11 at 10:34 PM

Seen: 1502 times

Last Updated: Jul 20 '11 at 01:07 PM