difference between getcomponent and getcomponentinparent. Parent - Children

hello, I have a confusing question about getcomponent and getcomponentinparent. I’m learning the unity and C# followed on video. and I made a mistake when I typed the code. at the beginning, the “get component” was typed into the program. when the software was played in unity, “NullReferenceException: Object reference not set to an instance of an object” error is active. and I tried the “getcomponentinparenent”, the program is working. I can’t understand what is difference between getcomponent and getcomponentinparent. in my mind they are both inherited component from parent.

Hello @zhjp

Its simple. Parent/Children Masterclass :smiley:

Imagine you have this 5 Objects in the Hicheracy:

>Object A1
    >Object B1
       >Object C1
    >Object B2
    >Object B3

(i will talk about the objects by its Letter+Number, B2 = Object B2)

Note that B1 is children of A1, and C1 is children B1. (B2 and B3 are also children of A1)
This also means, that A1 is the parent of B1, B2 and B3. And B1 is the parent of C1.
Also, A1 has no parent.

In code format, some examples, we say:

B1.transform.parent = A1.transform; // here we say "this objects parent is that object"
A1.transform.parent == null;

B2.transform.parent = B1.transform.parent;  //here we say, "this objects parent is that objects parent"
// Which is the same as
B2.transform.parent = A1.transform;

You see any object can have multiple children, but only 1 parent. (as in real life, thats why parent/children words are used in hicheracy)
So, if you want to access a component (Renderer for example) in Object B1 you can do it in 3 different ways:




All this 3 ways will reach the Renderer component of ObjectB1.

You must also know thatthere is GetComponent, and GetComponents , with the S.
GetComponentS returns an array or components. for example this:

Renderer[] RenderComponents = A1.GetComponentsInChildreen<Renderer>();

This will return an array of 3 renderers, rederer of B1, of B2 and of B3.

There are the 6 functions, the combination of:

  • children/“itself”/parent

  • 1 component / Array of components

So think whats the function you really need in each case!!


GetComponentInParent is simply this:

public static T GetComponentInParent<T>(this GameObject go)
    Transform current = go.transform;
    while (true)
        if (current == null)
            return null;
        T comp = current.GetComponent<T>();
        if (comp)
            return comp;
        current = current.parent;

It looks for the component on the given object and if it is not found on this gameobject, it goes up the hierarchy and tries it again on the parent. If the parent also does not have this component it continues to go up (the parent’s parent and so on). If the component can not be found at all after reaching the top of the hierarchy, it would return null.

If you get a null reference exception when using GetComponent but no error when using GetComponentInParent it means the component you’re looking for does not exist on the object you used GetComponent on but on one of the parent objects.

Note there’s also the opposite of GetComponentInParent which is called GetComponentInChildren. This method instead of walking up the hierarchy checks the nested objects (childs) of the given object if the component can not be found.

in my mind they are both inherited component from parent.

Note that a hierarchical relationship of gameobjects has nothing to do with type inheritance, even though terms like parent and child are used in both cases. Making a gameobject a child of another object does not change the inheritance of objects. Gameobject nesting should be thought of nesting of objects. So putting your phone into your pocket makes the phone a child of your trousers which is a child of you (in some way). However that doesn’t make the phone become a human, it is just “contained” / “attached” to a human and moves along with it.

The same is true for components. Components are simply attached to gameobjects. GameObjects themselfs are just dumb containers that do not do anything on their own. All they are are boxes which can contain components. Those components define what this gameobject can do.