So I built a entire canvas game with using GetComponentsInChildren alot.
I just realized on this new game i’m making that it actually searches through all children of children gameObjects.
I read i can do something like
foreach(Transform t in gameObject.transform)
but i have alot of scripts that rely on the stored array variable that GetComponentsInChildren creates.
so i would have to make a function to use that creates that.
I was just wondering is there another option?
If I use GetComponentsInChildren and the component im searching for only exists on the first child layer. That function is still looping through all children depths right? so its still be bad performance.
Bad for performance is relative so far as how often its used, eh. For calls when you know you need only 1, use the variation “GetComponentInChildren” which will return a singular occurence, if there?
As methos5k said, if you don’t use it often, GetComponentsInChildren isn’t bad. If you do need those components often, ideally you could store references to them inside the class.
As in, instead of using GetComponentsinChildren on every frame in Update(), use it once in Start() and store the results to in an array or generic List<> variable.
1 Like
i would never use something like that inside Update lol.
These things are used when you load a window with https call.
Ive seen some peformance issues on mobile platform and web GL
Hey Shadowing,
methos5k and TheWanderingBen both have great solutions. The only thing I would add is to use an iterator instead of foreach.
foreach is not recommended for run-time usage as it is much slower and creates unnecessary memory allocations (See Link).
Try something like this in the start function:
Transform[] tfArray = GetComponentsInChildren<Transform>();
for (int i = 0; i < tfArray.Length; i ++ )
{
doAction(tfArray[i]);
}
You know, I actually only use iterators in my code, but I’d forgotten why. Thanks for sharing!
For anyone visiting this thread now, using foreach is ok - it does not create unnecessary allocations anymore, and the difference in speed is negligible. I’ve read a few articles (such as C#: For Vs ForEach Vs While - The Curious Consultant ) that found foreach to be faster than a for-loop under certain conditions. I’m not sure how that’s possible, so more testing would be required to move me away from the traditional for-loop as a default, but I certainly wouldn’t hesitate to use foreach if you prefer.
13 Likes
To confirm what @mattparkins mentioned. This text below is from Unity Official Learning website. Please have a look at the link I attached below, you can search using Ctrl+F or Command+F and use ‘foreach’ keyword to search, then you will find the exact same text I pasted here.
"
In versions of Unity prior to 5.5, a foreach loop iterating over anything other than an array generates garbage each time the loop terminates. This is due to boxing that happens behind the scenes. A System.Object is allocated on the heap when the loop begins and disposed of when the loop terminates. This problem was fixed in Unity 5.5.
As long as you have Unity 2019.3 you are safe but if we are unable to upgrade our version of Unity, there is a simple solution to this problem. for and while loops do not cause boxing behind the scenes and therefore do not generate any garbage. We should favor their use when iterating over collections that are not arrays.
"
Link - Fixing Performance Problems - 2019.3 - Unity Learn