Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by hawken-2 · Sep 17, 2011 at 07:12 PM · camerainstantiateprefabtaglayer

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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";
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Sep 18, 2011 at 04:36 AM 0
Share

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.

avatar image hawken-2 · Sep 19, 2011 at 08:21 AM 0
Share

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

avatar image Adamcbrz · Sep 25, 2011 at 03:36 AM 1
Share

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.

avatar image
8

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image IgorAherne · Mar 19, 2017 at 02:53 AM 1
Share

works super well! just no need for : $$anonymous$$onobehavior and we also need to add using UnityEngine and using UnityEditor

thanks!

avatar image simonepostal · Aug 14, 2018 at 06:55 PM 0
Share

Thank you! It works perfectly!

avatar image
0

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image syclamoth · Sep 19, 2011 at 08:26 AM 1
Share

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.

avatar image
0

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);
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ls97 · May 26, 2015 at 09:59 AM 0
Share

how do you use a foreach loop when trans isn't a array?

avatar image g-pechorin · May 26, 2015 at 10:09 AM 0
Share

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)

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawning problems with cameras 1 Answer

Get transform of instantiated Prefab; Help appreciated. 1 Answer

Can't set camera target after instantiating a prefab(like respawn) 0 Answers

Prefab tag bug 0 Answers

Spawning random prefabs with the same tag 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges