Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
18
Question by Suyuanhan · Oct 14, 2011 at 06:20 AM · objectfindroot

A quick way to find the root parent?

For example,there's an object contains a lot of children,like:

Person -> Body -> Hands -> Finger(Add a box collider component for Finger object);

and when the Finger hit by a Bullet for example,

How to find "Person" object in a quickl way , not to use "collider.transform.parent.parent.parent" ect....

thx...

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

2 Replies

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

Answer by syclamoth · Oct 14, 2011 at 06:29 AM

Instead, you use the handy shortcut

 collider.transform.root;

This always returns the highest transform in the hierarchy!

Comment
Add comment · Show 4 · 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 PatrikNyblad_SI · Oct 14, 2011 at 06:42 AM 0
Share

Awesome, I did not know that! Ill add it to my solution also but the poster of this question should mark your answer as the right one!

avatar image Suyuanhan · Oct 14, 2011 at 07:41 AM 0
Share

Ha, This simple script shows me I'm lazy to look for the document!!!!

Thx!

avatar image _MGB_ · Jan 24, 2014 at 11:47 PM 0
Share

Note: it may not be ideal to use the root like this, as you come a cropper if you start parenting objects to others! Speaking from experience here ;)

avatar image TheWarper · Jul 06, 2016 at 01:01 AM 0
Share

brilliant!

avatar image
9

Answer by PatrikNyblad_SI · Oct 14, 2011 at 06:31 AM

My approach would have been to cache the Person Transform or GameObject in Finger.

 private var person : Transform;
 
 function Start () {}
     person = transform.parent.parent.parent;
 }


Another way to get the person would be to use GameObject.Find() like this:

 person = GameObject.Find("/Person");

But this assumes you have the Person object in the root of your scene since I put a "/" before the "Person" and this is just to make the find faster, otherwise it will search through your whole hierarchy of objects. This will of course limit you to only have one Person object in your scene.

GameObject.Find Manual: http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html

And just as a tips when developing for iPhone and getting components attached to objects through code witch i guess you want to do to apply damage to Person in a script, you should get the component like this:

 GetComponent.<ScriptName>();

Another even simple way to do things suggested by "syclamoth" is:

 transform.root

 

Hope it helps!

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 Suyuanhan · Oct 14, 2011 at 07:43 AM 0
Share

Thank you the same for such a detail description,it makes me learn more things.

avatar image lolo8304 · Jun 09, 2019 at 11:19 PM 0
Share

Great - root was the answer. perfect

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

find object in game view 1 Answer

How do i GET the the YRotation of an object? 4 Answers

How you find an Object 1 Answer

How could I get a selection set of GameObject sharing a material or a script in Editor? 1 Answer

How to find a child from root? 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