Search Unity

  1. Click here to see what's on sale for the "Best of Super Sale" on the Asset Store
    Dismiss Notice
  2. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

GetComponents/InParent/InChildren list overload not documented

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

  1. Baste

    Baste

    Joined:
    Jan 24, 2013
    Posts:
    5,168
    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!
     
    efge and landon912 like this.
  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:
    114
    I believe this is still yet to be done...
     
  5. icefallgames

    icefallgames

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

    ModLunar

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

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    814
    4 years...
     
    daxiongmao likes this.
  8. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    296
    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);
     
    ModLunar likes this.
  9. ModLunar

    ModLunar

    Joined:
    Oct 16, 2016
    Posts:
    204
    @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.
unityunity