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?

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 +
                        "

(" + (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 :slight_smile:

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

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

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…?

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);
}