Working around a limitation in Unity, it would be very helpful if GetComponents returns them in a reliable ordering. Is this guaranteed?
Iām assuming the order of components returned is either the top-most to bottom-most on the GameObject, or the first-added to last-added component on the GameObject.
This is just a blind guess though; some testing would have to be done.
If itās not documented as being guaranteed, then itās not guaranteed.
It can change at any time in future versions of Unity, or change from target build to target build.
Itās not documented as such, so no.
The order of returned components from GetComponents<> is the order they are in the inspector and is reliable. My own custom network APIās RPC system depends on this being the case, and I havenāt seen an instance of it yet failing to be so. (Iām currently still on 2018.2.21, so hopefully they havenāt broken this since then, but I doubt it)
I stand by my statement of āguaranteeā, since guarantee implies that it has been stated as such by Unity. Which it hasnāt been.
But I have to say if youāre getting reliable results Iāll have to play around with it. The last time I ran tests on it was a few years ago when I was doing some ordering in my t&i system and I found different orders from build to build, especially between target platforms.
I believe in the long long ago it was not reliable. Not sure when that changed. Iām seeing the ordering is the same on Linux and Windows, but havenāt checked other platforms.
From the current documentation:
https://docs.unity3d.com/Manual/UsingTheInspector.html
Yeah they should put this on the GetComponents scripting reference page, instead of the last thing tagged onto the Inspector manual page, but there it is.
Nice find.
I think itās worth mentioning that under ideal circumstances, you should probably attempt to design your program in such a way that the order of the components does not matter.
In order to develop new networking system they made this order reliable, but before it wasnāt. There was article about this in Unity blog.
Thanks for the info!
Iād prefer to go a completely different route with this system but cannot due to a limitation in Unityā¦but thatās an entire other can of worms.
Since the topic was already bumped, the order of the components was made reliable when the new audio filter system was introduced. Thatās because audio filters use the order to āstackā themselfs. See the documentation:
I think it was somewhere around Unity 5.2, so about 8 years ago.
The reliable order was also necessary for nested Prefab enhancements in Unity 5; hence the warnings ever since that you could not re-order the components of a Prefab. (A limitation which may be overcome soon, likely with lots of bandages and paperclips and sealing wax.)
I think 5.2 was in 2017, so about 6 years ago.
5.0 was in 2015.
That time would also make sense relative to my results I was talking about in my old posts above. When I began developing that t&i system was pre-5.2.
Yes, possible. I donāt have a track record of the Unity releases Though according to the release notes 5.2 was released on August 16, 2015. Iām not sure how reliable those dates areā¦
Agreed⦠I cantā really keep track of all of it. Iām actually upset theyāre going back to the numbering rather than year naming model. At least the year numbering SORT of helped in knowing when it came out (though of course LTS confuses that further).
For all I know it could have been late 2015. I google and got different results⦠but that just means different people are saying different things.