GetComponentInParent, how does it work?

So this works perfectly

Character= transform.parent.GetComponent<Transform>();

But this doesnt

Character = GetComponentInParent<Transform>();	

So my question is simple, what is the exact syntax to make GetComponentInParent work?

Even though i never used GetComponentInParent i guess it does work similar to GetComponentInChildren. What most don’t know is that it starts searching on the given object and not on the first child or in the case of GetComponentInParent on the parent. So using this method to find a Transform will return the Transform of the object and not of the parent.

Both methods are actually badly named. They should be called something like:

  • FindComponentUpwards == GetComponentInParent
  • FindComponentDownward == GetComponentInChildren

If you just want to access the transform of the direct parent object, use:

    Character = transform.parent;

If you want to access the top most parent, use:

    Character = transform.root;

Your question says that “it doesn’t work”, but that does not provide any useful information regarding what actually happens when you run that code.

Here is a test scene with a root-child object:


On the child i placed a script that runs your code exactly and prints out the returned component to the console.

var t1 = transform.parent.GetComponent<Transform>();
var t2 = GetComponentInParent<Transform>();

Debug.Log (t1);
Debug.Log (t2);

This is what the debug output:


From this we can determine that GetComponentInParent looks for components upwards in the hierarchy STARTING WITH ITSELF (this is not specifically written in the documentation page here: Unity - Scripting API: GameObject.GetComponentInParent), it is only specified that the method recurses upwards to look for the requested component.

Taking a peek at this method’s code through the help of Reflector reveals that indeed it first looks for the component on the current game object, before moving upwards.

So is there a way to access the root from the child using this ?

Transform parentTransform = GetComponentInParent();

This is an old question, but as I just ran into this problem, I’ll share my solution. Quite simply, since the existing methods start the search at the current GameObject, these methods start the search on the immediate parent or children.

To find the first occurrence of a Component (MonoBehaviour) in any Parent of the current object:

        public static T ReallyGetComponentInParents<T>(MonoBehaviour obj) where T : MonoBehaviour
            if (obj.transform.parent != null)
                return obj.transform.parent.GetComponentInParent<T>();
            return null;

To find the all occurrences of a Component (MonoBehaviour) in the descendants of the current object:

    public static List<T> ReallyGetComponentsInChildren<T>(MonoBehaviour obj) where T : MonoBehaviour
        List<T> found = new List<T>();
        for (int index = 0; index < obj.transform.childCount; ++index)

        return found;

Thanks to @Bunny83 and @liortal for excellently describing the problem.