Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
5
Question by Dan 9 · Sep 29, 2010 at 03:23 PM · hierarchyuptraverse

Traverse up the hierarchy to find first parent with specific tag

I have a child object that needs to find the first parent object with a certain tag.

Parent Object ("Foo tag") \ Child Object \ Child Object | Child Object | Child Object \ Child Object (needs to traverse up the tree until it finds the "Foo tag"

I can't just do a find for the foo tag because I have multiple objects with that tag - I need to find the one that is a parent of this child.

Comment
Add comment · Show 2
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 Dan 9 · Sep 29, 2010 at 03:25 PM 1
Share

I could loop through each parent until I find the tag. parent = transform.parent while (parent != null) { if (parent.tag == "Foo tag")... }

avatar image Mike 3 · Sep 29, 2010 at 03:39 PM 1
Share

That's exactly how you'll need to do it

5 Replies

· Add your reply
  • Sort: 
avatar image
16

Answer by TruffelsAndOranges · Jun 20, 2015 at 09:20 PM

Here is a solution in C# (tested and working in Unity 5.1):

 public static GameObject FindParentWithTag(GameObject childObject, string tag)
 {
    Transform t = childObject.transform;
    while (t.parent != null)
    {
       if (t.parent.tag == tag)
       {
          return t.parent.gameObject;
       }
       t = t.parent.transform;
    }
    return null; // Could not find a parent with given tag.
 }


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 movAX13h · Aug 19, 2015 at 03:03 PM 1
Share

line 10: "t.parent.transform" is the same as "t.parent"

avatar image TruffelsAndOranges · Aug 19, 2015 at 03:07 PM 0
Share

I think using "t.parent.transform" makes the code easier to read for most people.

avatar image WaqasHaiderDev · Jun 01, 2020 at 04:22 PM 0
Share

Working perfect in 2020. Testes with Unity 2018.4. Thanks for sharing.

avatar image
1

Answer by Dan 9 · Sep 29, 2010 at 04:03 PM

See comments above for answer. It is working great.

Comment
Add comment · 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
1

Answer by bladnman · Apr 07, 2012 at 06:06 PM

I hope this helps someone -- and please correct me where I'm wrong!

 // FIND PARENT WITH TAG
 function findParentWithTag(tagToFind:String) {
     return findParentWithTag(tagToFind, this.gameObject);
 }
 function findParentWithTag(tagToFind:String, startingObject:GameObject) {
     var parent = startingObject.transform.parent;
     while (parent != null) { 
         if (parent.tag == tagToFind) {
             return parent.gameObject as GameObject;
         }
         parent = parent.transform.parent;
     }
     return null;
 }




Comment
Add comment · 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
1

Answer by chatbear69 · Jul 15, 2015 at 10:45 AM

(Tested and working in Unity 5.1)

 var elevatorComponent : Transform;
 
 function Start() {
 
     elevatorComponent = transform.parent.parent;
 
 }
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 Xepherys · Jan 14, 2017 at 06:26 PM 2
Share

This is only a good solution if there are definitely two levels that you want to traverse up. The OP specified that there may be varying levels up the tree before finding the tag.

This isn't a great answer to the question at all.

avatar image
-1

Answer by NiMareQ · Sep 16, 2012 at 05:45 PM

Imagine a form with three uls (as sections). Each section has several 'li checkbox /li' and the last one says Check/Uncheck All. Here is the code, which will change all checkboxes in same ul.

 $(document).ready(function(){
   $('.check-all').change( function(){
     $(this).closest("ul").find('input[type=checkbox]').attr('checked', $(this).attr('checked') || false );
   });
 });
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do NetworkIdentity and Hierarchy interact? 0 Answers

Particle System playing spontaneously 1 Answer

Unity hierarchy not loading and flashes around. 0 Answers

NavMesh and WaveSpawner dont work together! 2 Answers

Preventing "zombie fish"? A question about stretched particles. 1 Answer


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