GetComponentsInChildren

I would like to identify Objects by index across a Network-Connection. Will GetComponentsInChildren always give me the same order of Components?

I could of course order the Components by their instance id returned by GetInstanceID(). But will this number be guaranteed to be the same on different machines or is it generated somehow each time the game is started?

If this is not the case, is there any other way to identify an object unanmbigously soley through Scripting so our Level-Designer doesn’t have to bother with giving each GameObject a unique number as a variable?

Thanks

1 Like

instance id’s and the order of components are both unreliable; the best way to do it is to make sure all the gameobjects have unique names.

The instance ID would be created when the object is created by the engine at startup, so the results you get are dependent on how the engine decides to number the instances, and any change to the game in any way would change the results. (relying on that across two seperate clients is a bad, evil plan) :wink:

The best way I can think of is using the name attribute if it’s an instatiated GO, otherwise a script component with a name or index var may be the way to go.

I may be missing something though. Unity usually has a simpler way hiding somewhere.

-Jeremy

Edit: Ray beat me to it.

Thanks for your replys. I feared this might be the case.

Using the names of the GO is error-prone because they are not guaranteed to be unique. The same goes for the self implemented id-numbers.

I guess I could also tell our Level-Designer to make all the objects of this type be children of each other. If each GO only has one child, then the order would be identical across every machine. But it seems kind of odd to do this and very unpractical.

Any thoughts?

GetComponentsInChildren returns you the same array on every machine, thus you can safely use it for networking.

Instance ids are not necessarily the same as they depend on previosly loaded levels and instantiated objects. But you dont need it for this purpose.

8 Likes

Thank you very much for this info! This makes everything much easier.