Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice

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

Discussion in '2021.2 Beta' started by Kichang-Kim, Apr 26, 2021.

  1. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,011
    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.
     
  2. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,011
  3. Deleted User

    Deleted User

    Guest

    Could you post code examples please? Don't forget to use the code tags, thank you.
     
  4. Kichang-Kim

    Kichang-Kim

    Joined:
    Oct 19, 2010
    Posts:
    1,011
    Code (CSharp):
    1. class MyComponentA : MonoBehaviour {}
    2. class MyComponentB : MonoBehaviour
    3. {
    4.     void Reset()
    5.     {
    6.         var componentA = this.GetComponentInParent<MyComponentA>();
    7.  
    8.         Debug.Log(componentA == null); // componentA will be null if you add MyComponentB to object via Project View inspector.
    9.     }
    10. }
    11.  
    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:
     
    LeonhardP and Deleted User like this.