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…

Instead, you use the handy shortcut

collider.transform.root;

This always returns the highest transform in the hierarchy!

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!

how do i get the collider.tranform.root-1 ?

transform.GetComponentInParent<ComponentName>().transform

this will search through all of the parents that this child is attached to