(Case 1331778) Component.GetComponentInParent() makes different result in specific situation

Hi. I found strange issue related to Component.GetComponentInParent().

When using Component.GetComponentInParent() in MonoBehaviour.Reset() method, it makes different result.

  1. When adding component via Project View’s inspector (select prefab asset in project view), it return null.
  2. When adding component via Hierarchy View’s inspector (double click prefab asset and enter Prefab Scene mode), it return component correctly.

Reproducible project was sent as Case 1331778.

Could you post code examples please? Don’t forget to use the code tags, thank you.

class MyComponentA : MonoBehaviour {}
class MyComponentB : MonoBehaviour
{
    void Reset()
    {
        var componentA = this.GetComponentInParent<MyComponentA>();

        Debug.Log(componentA == null); // componentA will be null if you add MyComponentB to object via Project View inspector.
    }
}

When you add MyComponentA and MyComponentB sequentially, it shows false if you are in Prefab Scene mode. But it shows true if you are inspecting via Project View.

Here is Unity’s comment:

2 Likes