Search Unity

GetComponentsInChildren

Discussion in 'Scripting' started by Der Dude, Apr 14, 2007.

  1. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    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
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    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.
     
  3. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    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) ;-)

    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.
     
  4. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    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?
     
  5. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    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.
     
  6. Der Dude

    Der Dude

    Joined:
    Aug 7, 2006
    Posts:
    213
    Thank you very much for this info! This makes everything much easier.