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?
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 ![]()
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.
– syclamothI 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.
– AdamcbrzThanks 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.
– syclamothYou 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)
works super well! just no need for
– IgorAherne: Monobehaviorand we also need to addusing UnityEngineand usingUnityEditorthanks!Thank you! It works perfectly!
– simonepostal