x


Is there an easy way to apply the same tag to all children of an object?

Basically this:

gameObject.tag = "theTag";

On a prefab, needs to tag all the children in the prefab with that tag, without knowing their names specifically.

Is it possible?

more ▼

asked Sep 17 '11 at 07:12 PM

hawken gravatar image

hawken
91 19 24 25

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

Not tested, but this should work (C#):

foreach(Transform t in transform)
{
    t.gameObject.tag = "theTag";
}
gameObject.tag = "theTag";
more ▼

answered Sep 18 '11 at 03:06 AM

sven1994 gravatar image

sven1994
291 2 3 10

Won't work on deep hierarchies- Transform[] only works on the layer directly below, it doesn't work recursively. Try using GetComponentsInChilderen<.Transform>() (without the . infront of the Transform (silly html tags having the same format as generics)) - this returns an array of all the transforms below the current transform.

Sep 18 '11 at 04:36 AM syclamoth

Thanks, feel like I've learned something from this. Would be nice if layers and tags were components eh?

Here's the final JS:

for (var child : Transform in transform) { child.tag = "theTag"; }

Sep 19 '11 at 08:21 AM hawken

I had the same issue and found it doesn't help when going past the first level of children. What I ended up doing was a recursive loop to solve the issue.

C# void OnDrawGizmosSelected () { ReTag(transform, transform.tag); }

void ReTag (Transform _transform, string tag) { foreach(Transform child in _transform){ child.gameObject.tag = tag; ReTag(child, tag); } }

Thought I would post for anyone else that might need it.

Sep 25 '11 at 03:36 AM Adamcbrz
(comments are locked)
10|3000 characters needed characters left

You can easily achieve that with a recursive method:

    AddTagRecursively (transform, "theTag");

void AddTagRecursively(Transform trans, string tag)
{
    trans.gameObject.tag = tag;
    if(trans.GetChildCount() > 0)
       foreach(Transform t in trans)
         AddTagRecursively(t, tag);
}
more ▼

answered May 22 at 04:43 AM

jsanz gravatar image

jsanz
1 1

(comments are locked)
10|3000 characters needed characters left

Thanks for this, I'll check if it works. My initial feeling is that you can't access grandchildren with GetComponents, because Unity throws an error as tags are not components...?

more ▼

answered Sep 19 '11 at 12:57 AM

hawken gravatar image

hawken
91 19 24 25

No, you use GetComponents, and then for each component returned use

component.gameObject.tag

to fix up the tag! No, tags are not components, but it is possible to access the tag of any object with just a reference to one component on that object.

Sep 19 '11 at 08:26 AM syclamoth
(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:

x3003
x1674
x1257
x323
x106

asked: Sep 17 '11 at 07:12 PM

Seen: 938 times

Last Updated: 1 hour ago