Search Unity

GetComponentsInChildren(false) returns inactive objects

Discussion in 'Editor & General Support' started by Peter77, Oct 21, 2017.

  1. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
    I was using GetComponentsInChildren<MyComponent>(includeInactive=false) and to my surprise, it also returns components that are added to deactivated game objects.

    Looking at the documentation, it says:
    According to the documentation and common sense from the naming, passing includeInactive=false should not include Components on deactivated game objects.

    I was going to create a bug-report, because GetComponentsInChildren(false) does not work as expected and documented.

    Then I found the following bug-report in the public issue tracker, describing the same problem I just ran into:
    https://issuetracker.unity3d.com/is...-always-gets-components-from-inactive-objects

    The bug-report was closed with "By Design", without further note.

    @LeonhardP How can this be the expected behavior? This must be a mistake, right?
     
    a436t4ataf likes this.
  2. LeonhardP

    LeonhardP

    Unity Technologies

    Joined:
    Jul 4, 2016
    Posts:
    3,136
    Hey Peter,
    I'm investigating. Stay tuned, but it might take a while since the dev who introduced this behaviour is currently unavailable.
     
    Peter77 likes this.
  3. Lucas-Meijer

    Lucas-Meijer

    Unity Technologies

    Joined:
    Nov 26, 2012
    Posts:
    175
    Hey Peter,

    The behaviour is by design, allthough your surprise is not unreasonable. We should clarify the documentation.

    The "includeInactive" setting is only used to consider which children we will traverse. The direct object you're invoking the GetComponentsInChildren() call is always scanned. In the very very very old days this was not the behaviour, and many people were rightfully very surprised by that as well, especailly since in the editor, a prefab is just an inactive gameobject, it meant that if you asked a prefab for its GetComponentsInChildren you would always get back an empty array.

    Hope that clears things up a bit, Lucas
     
    Peter77 likes this.
  4. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,620
    Yes it does, thanks for the explanation!
     
  5. a436t4ataf

    a436t4ataf

    Joined:
    May 19, 2013
    Posts:
    1,933
    Just got bitten by this ... in the latest 2019.3.x release :(.

    Took multiple people reading the code to eventually realise that the source code flag doesn't do what the name implies it does.

    Is there any chance this flag could be renamed? It's really the wrong name.