Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only.

    Please, do not make any changes to your username or email addresses at id.unity.com during this transition time.

    It's still possible to reply to existing private message conversations during the migration, but any new replies you post will be missing after the main migration is complete. We'll do our best to migrate these messages in a follow-up step.

    On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live.


    Read our full announcement for more information and let us know if you have any questions.

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,371
    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

    Administrator

    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:
    233
    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:
    376
    Haha.. was wondering the same thing. Happy 2020 XD
     
  7. bdovaz

    bdovaz

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

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    413
    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:
    376
    @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:
    376
    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:
    153
    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:
    187
    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,204
    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:
    1,068
    6 years...
     
    Bizquit110 likes this.
  17. strich

    strich

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

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    12,015
    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, Bizquit110, Baste and 3 others like this.
  20. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    376
    YES!

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

    Baste

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

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