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
1
Question by doublethink · Jan 16, 2013 at 03:35 PM · getcomponentrootgetcomponentinchildren

transform.root and empty root game objects don't play nice

A handful of my scripts need to use transform.root due to nested colliders in their parent object. This is not a problem when the base object contains the component I am trying to retrieve, typically code like below after a succesful raycast hit.

 EnemyHealth e;
 e = hitInfo.collider.transform.root.GetComponent<EnemyHealth>();
 e.SomeMethod(value);

But now I've decided I have too many objects in my scene and I would like to group them into empty game object 'folders' as per most of the community's agreement. Subsequently the above code is broken because root now references an empty game object. I tried doing this,

 EnemyHealth e; 
 e = hitInfo.collider.transform.GetComponentInChildren<EnemyHealth>();
 e.SomeMethod(value);

but it doesn't always find the component. Is there a way to use transform.root and empty root game objects correctly? If not, is there an alternative to using transform.root in my case?

Comment
Add comment · Show 1
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 doublethink · Jan 28, 2013 at 04:41 PM 0
Share

Was really hoping for an answer to this!

1 Reply

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

Answer by Bunny83 · Jan 28, 2013 at 04:59 PM

Sure, use your own "root"

Here are some extension methods i just wrote from scratch:

 public static Transform GetRoot(this Transform aObj, int aLevel)
 {
     var tmp = aObj;
     var list = new List<Transform>();
     while(tmp != null)
     {
         list.Add(tmp);
         tmp = tmp.parent;
     }
     list.Reverse();
     return list[Mathf.Clamp(aLevel,0,list.Count)];
 }
 
 public static T GetNextParentComponent&lt;T&gt;(this Transform aObj) where T : Component
 {
     var tmp = aObj;
     while(tmp != null)
     {
         var comp = tmp.GetComponent<T>();
         if (comp != null)
             return comp;
         tmp = tmp.parent;
     }
     return null;
 }
 
 public static Transform FindParent(this Transform aObj, string aName)
 {
     var tmp = aObj;
     while(tmp != null)
     {
         if (tmp.name == aName)
             return tmp;
         tmp = tmp.parent;
     }
     return null;
 }

Just place them in a static class somewhere. The usage should be clear, but just in case:

 hitInfo.collider.transform.GetRoot(0);  // returns the same as .root
 hitInfo.collider.transform.GetRoot(1);  // returns one object before the actual root
 hitInfo.collider.transform.GetRoot(2);  // and so on...
 
 // If you have a certain script on the "root" object use
 hitInfo.collider.transform.GetNextParentComponent&lt;MyScript&gt;();
 
 // Find the nearest parent that is named "MyParent"
 hitInfo.collider.transform.FindParent("MyParent");

edit
Fixed the generic parameters which aren't displayed since the last update. Replaced them with lt / gt

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 Bunny83 · Jan 28, 2013 at 05:07 PM 0
Share

[$$anonymous$$eta] It's getting worse.... Now angle brackets are recognised for "List" and "GetComponent" but not on custom generics... Even worse if i replace all brackets with lt / gt the ones after GetComponent and List aren't displayed... Now we have pure inconsistency.

avatar image doublethink · Jan 29, 2013 at 09:19 PM 0
Share

You wrote "methongs" ins$$anonymous$$d of "methods" lmao, thanks for the answer!

avatar image doublethink · Jan 29, 2013 at 09:41 PM 0
Share

Also, many kudos for creative use of $$anonymous$$athf.Clamp. Is there anything clamp can't do?

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

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

GetComponentInChildren Returning a Component in Parent 4 Answers

OnBecameVisible() .. GetComponent(ScriptName) 0 Answers

enemy attack not recognizing players stats 1 Answer

Access component of parent with multiple children 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