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.

GetComponents/InParent/InChildren list overload not documented

Discussion in 'Documentation' started by Baste, Nov 1, 2016.

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,103
    GetComponents and GetComponentsInChildren and GetComponentsInParent have these overloads available:

    Code (csharp):
    1. public void GetComponentsInParent<T>(bool includeInactive, List<T> results);
    2. public void GetComponentsInChildren<T>(bool includeInactive, List<T> result);
    3. public void GetComponents<T>(List<T> results);
    4.  
    5. //also this one, for when you hate writing good code:
    6. public void GetComponents(Type type, List<Component> results);
    None of these methods are mentioned anywhere in the docs.

    Doing a quick check, it seems like the methods clear the provided list, and then put the results in it. It seems like this is meant as a gc-free alternative to GetComponents (yay!). Using a List<T> instead of an array with an int return that defines the count of elements also makes this a much better solution to the gc problem than eg. Physics.RaycastAll and NavMeshPath.GetCornersNonAlloc!

    It'd be nice to have clear docs on what the method does, though!
     
  2. Graham-Dunnett

    Graham-Dunnett

    Unity Technologies

    Joined:
    Jun 2, 2009
    Posts:
    4,287
    Added into the doc team list of work to do. It's common for functions to have overload versions which need documentation. :-(

    (Thanks for all the comments and observations btw.)
     
  3. tsomda

    tsomda

    Joined:
    Mar 15, 2017
    Posts:
    1
    It seems like it is not on the documentation yet. :(

    One of these overloads is really helpful to me!
    It makes the code much cleaner.

    not very clean:
    myScriptList= new List<MyScript>(GetComponents<MyScript>());

    much cleaner:
    GetComponents<MyScript>(myScriptList);

    :)
     
  4. chadfranklin47

    chadfranklin47

    Joined:
    Aug 11, 2015
    Posts:
    210
    I believe this is still yet to be done...
     
  5. icefallgames

    icefallgames

    Joined:
    Dec 6, 2014
    Posts:
    75
    Still not documented... would be nice to have confirmation that this clears the list before adding components.
     
    MilenaRocha and ModLunar like this.
  6. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    359
    Haha.. was wondering the same thing. Happy 2020 XD
     
  7. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    988
    4 years...
     
  8. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    387
    The Component version of the docs does not have it.
    The GameObject version does.

    But it does not say the list is cleared. Which seems a waste. Not sure why i would want to use the list version if its cleared. If i want to collect a bunch of components into a single list i still have to append them to another list.

    I would expected i would of been able to do this.
    goA.GetComponentsInChildren<Renderer>(true, _TmpRenderers);
    goB.GetComponentsInChildren<Renderer>(true, _TmpRenderers);
     
    TimoDazzleRocks and ModLunar like this.
  9. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    359
    @daxiongmao Funny you mention that -- I needed to accumulate a bunch of components like that -- so having it NOT be cleared (and mentioning this in the docs) would be super helpful for our sanity and the computer's performance.
     
    daxiongmao likes this.
  10. ProdiggaPA

    ProdiggaPA

    Joined:
    Nov 29, 2019
    Posts:
    11
    I came here because I was trying to figure out if the List gets cleared. Docs still missing.
     
    shibaharu0325, bartofzo and ModLunar like this.
  11. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    359
    Okay I don't mean to be rude, but it is 2021.

    Will this ever get documented?
    (and an extra overload?)

    Pretty please? <3
     
    owlrazum and bartofzo like this.
  12. bartofzo

    bartofzo

    Joined:
    Mar 16, 2017
    Posts:
    140
    Same thing for me. It seems the list is cleared, so unfortunately there isn't much use.
     
  13. Zapan15

    Zapan15

    Joined:
    Apr 11, 2011
    Posts:
    174
    It get's cleared (which I do not like). Would be nice if the list will not be cleared.We overcome that with 2 internal lists now, however, this is a bit hacky!
     
  14. gilley033

    gilley033

    Joined:
    Jul 10, 2012
    Posts:
    1,101
    It gets cleared because Unity think we are too stupid to clear it ourselves. It'd be nice if Unity had some faith in us.
     
    bartofzo, owlrazum and ModLunar like this.
  15. s-sixteen

    s-sixteen

    Joined:
    Aug 22, 2015
    Posts:
    7
    Bump? Docs are still missing...
     
  16. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    988
    6 years...
     
    Iq110 likes this.
  17. strich

    strich

    Joined:
    Aug 14, 2012
    Posts:
    336
    The eternal counter continues to rise. Still no documentation or fix for the list clearing.
     
  18. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    9,958
    I mean, that's roughly the rate the docs get fixes. It would probably go faster if instead of actual docs all we wanted was silly sports car analogies.
     
  19. duck

    duck

    Unity Technologies

    Joined:
    Oct 21, 2008
    Posts:
    358
    hello! wow this is a bit overdue isn't it This landed on my desk in January, and I'm pleased to say the docs have now been updated. I ended up updating the docs on the whole "GetComponent" family (both on the Component and GameObject class) which includes:

    Component.GetComponent
    Component.GetComponentInChildren
    Component.GetComponentInParent
    Component.GetComponents
    Component.GetComponentsInChildren
    Component.GetComponentsInParent
    Component.TryGetComponent

    GameObject.GetComponent
    GameObject.GetComponentInChildren
    GameObject.GetComponentInParent
    GameObject.GetComponents
    GameObject.GetComponentsInChildren
    GameObject.GetComponentsInParent
    GameObject.TryGetComponent

    They now all prioritize the generic versions at the top, with the older and less efficient typeof & string overloads listed afterwards. It now explains how the versions work where you pass in a List for the results (as requested by Baste in 2016!) The code samples remain similar but with a few improvements and corrections, and in general all the pages are more consistent with each other, and mention the relevant alternatives where appropriate.
     
    Ghat-Smith, Iq110, Baste and 3 others like this.
  20. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    359
    YES!

    Thanks for all your effort @duck, this makes me very happy to see :)
     
  21. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    6,103
    Great job!

    The docs are well-written now, and will help future users a bunch!