- Home /
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?
Answer by sven1994 · Sep 18, 2011 at 03:06 AM
Not tested, but this should work (C#):
foreach(Transform t in transform)
{
t.gameObject.tag = "theTag";
}
gameObject.tag = "theTag";
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.
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"; }
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.
Answer by Johannski · Jun 21, 2015 at 07:25 PM
I made a small editor script, which will add an entry to the gameobject dropdown menu: using UnityEngine; using UnityEditor; using System.Collections;
public class ChangeChildTags : MonoBehaviour {
[MenuItem("GameObject/Change Children to Parent Tag")]
public static void ChangeChildrenTags()
{
GameObject currentObject = Selection.activeGameObject;
string parentTag = currentObject.tag;
if (currentObject != null && currentObject.transform.childCount > 0)
{
if(EditorUtility.DisplayDialog("Change child tags to parent tag", "Do you really want to change every child tag to " + parentTag + "?", "Change tags", "Cancel"))
{
Transform[] transforms = Selection.GetTransforms(SelectionMode.Deep | SelectionMode.Editable);
float numberOfTransforms = transforms.Length;
float counter = 0.0f;
foreach (Transform childTransform in transforms)
{
counter ++;
EditorUtility.DisplayProgressBar("Changing tags", "Changing all child object tags to " + parentTag +
"\n (" + (int)counter + "/" +(int)numberOfTransforms + ")",
counter/numberOfTransforms);
childTransform.gameObject.tag = parentTag;
}
EditorUtility.ClearProgressBar();
}
}
}
}
Just create a class ChangeChildTags in the Editor folder. To change the children, select a gameobject and change the tag there. Then click on GameObject -> Change Children to Parent Tag and the script will do the rest :)
works super well! just no need for : $$anonymous$$onobehavior
and we also need to add using UnityEngine
and using UnityEditor
thanks!
Answer by hawken-2 · Sep 19, 2011 at 12:57 AM
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...?
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.
Answer by jsanz · May 22, 2013 at 04:43 AM
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);
}
C# should be "smart enough" to work out how to iterate over an array or an iterator
(... is that if
statement needed? I'd avoid that if
statement and let unity execute the loop 0 times)