Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

GetComponentInParent doesn't behave like I'd expect

Discussion in 'Editor & General Support' started by invicticide, Jun 2, 2014.

  1. invicticide

    invicticide

    Joined:
    Nov 15, 2009
    Posts:
    109
    The documentation for Component.GetComponentInParent states:

    When called on an instantiated object, this behaves as written: it will return the requested component whether that component exists on the target object itself OR in any of the target object's parents.

    When called on a non-instantiated prefab, this function DOES NOT WORK. It appears to return null always, regardless of the layout of the prefab. It doesn't matter whether the target component is at the prefab root or in one of its children, nor does it matter whether I call GetComponentInParent on the prefab root or on one of its children, or any combination thereof.

    If I simply call regular ol' GetComponent on the prefab, the component is returned correctly. I can also use this to manually walk up the prefab hierarchy and find components like you'd expect. So I can set up a prefab that looks like this:

    Code (csharp):
    1.  
    2. Root
    3.     Child 1
    4.         Child 2
    5.  
    Then I can write some code like this:

    Code (csharp):
    1.  
    2. for(Transform t = testPrefab.transform; t != null; t = t.parent)
    3. {
    4.     T test = t.GetComponent<T>();
    5.     if(test != null)
    6.         return test;
    7. }
    8.  
    And I can call that code on Child 2 of that non-instantiated prefab, and it'll correctly find the desired component whether it's on Child 2, Child 1, or Root.

    It'd be really sweet if GetComponentInParent would work in a consistent/expected way when used on prefabs, especially since there doesn't appear to be anything functionally preventing this. :|
     
    Ladace likes this.
  2. Jodon

    Jodon

    Joined:
    Sep 12, 2010
    Posts:
    434
    I can verify this on my side, using the naive implementation (or in my case, NGUI's implementation) works as expected.
     
  3. julian-moschuering

    julian-moschuering

    Joined:
    Apr 15, 2014
    Posts:
    529
    Passing includeInactive=true to GetComponentsInParent should do the trick. The same is true for GetComponentsInChildren.
     
  4. bourriquet

    bourriquet

    Joined:
    Jul 17, 2012
    Posts:
    181
    Shouldn't that be considered a bug and reported as such?
     
    Orimay likes this.
  5. Orimay

    Orimay

    Joined:
    Nov 16, 2012
    Posts:
    304
    And GetComponentsInChildren work on Prefab just fine without "includeInactive = true". I think this should be fixed.
     
  6. nzhangaudio

    nzhangaudio

    Joined:
    May 26, 2015
    Posts:
    20
    second this.
     
  7. Maisey

    Maisey

    Joined:
    Feb 17, 2014
    Posts:
    300
    This is still an issue in 2019.4.8 (not critical enough for me to spend my time submitting a bug report though).
     
  8. FlaSh-G

    FlaSh-G

    Joined:
    Apr 21, 2010
    Posts:
    212
    RemDust, echoAndecho and sarynth like this.