Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can I use InstanceID to access an object

Discussion in 'Editor & General Support' started by AdrianMallon, Aug 20, 2008.

  1. AdrianMallon

    AdrianMallon

    Joined:
    Apr 11, 2008
    Posts:
    60
    Can I use an InstanceID to access a specific object (for example, to set the transform on gameObject 4928)? I don't think I can, but I wanted to make sure. Thanks, AM.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Not directly, but you can use the InstanceID to rename the object, and then do a GameObject.Find using the InstanceID to access the object that way. (Can you tell I've done this? ;) )

    --Eric
     
  3. AdrianMallon

    AdrianMallon

    Joined:
    Apr 11, 2008
    Posts:
    60
    How do you do that, Eric5h5? I can't see how to use InstanceID in any way, other than to compare two objects actively encountered. Unless you mean I use a routine to access every object in a project and give each a unique name or give an attached variable a unique identifier.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    A couple of different ways...you could do

    Code (csharp):
    1. gameObject.name = GetInstanceID().ToString();
    and then you can do

    Code (csharp):
    1. var foo = 38375; // A made-up InstanceId...in actual code you'd get this from a real object...you can store the instance ids in an array or something
    2. GameObject.Find(foo.ToString()).transform.position = whatever;
    I've also had a hashtable and used the instance ids as keys for the hashtable in order to access each object. You can go through and assign objects an identifier yourself, but since every object has a unique InstanceID() anyway, might as well use it if the situation calls for it.

    --Eric
     
  5. zumwalt

    zumwalt

    Joined:
    Apr 18, 2007
    Posts:
    2,287
    If this version of Mono they are using supports GUID, I would recommend using that for your random ID.
     
  6. AdrianMallon

    AdrianMallon

    Joined:
    Apr 11, 2008
    Posts:
    60
    Thanks Eric5h5
    I used your suggestion and things work. What a pity, though, the InstanceID doesn't function as a tag, because I had to create workarounds for the limitation that GameObject.Find() only finds enabled objects and can't find bodies that have been disabled.
    (I don't get the significance of Mono GUIDs, zumwait)
     
  7. se7en

    se7en

    Joined:
    Dec 3, 2008
    Posts:
    232
    Thanks Eric - I have been loosely trying to figure this out for years now : ) I always just give up and move on! In Flash programming we always rename duplicates with a unique ID and I hate that Unity names everything (clone) - it would be better if it used the Maya or Adobe protocol of just adding a sequential digit.

    With you help I can now get rid of "clone and add a unique ID!! Awesome.
     
  8. yoyo696

    yoyo696

    Joined:
    Aug 11, 2015
    Posts:
    14
    Eric5h5 I didnt know what happenned few weeks ago and I saw a mysterous thing in Unity that I didnt know to deal with it most of the weird things that I didnt know was that there is a localinstance or loacalid
    or localrotation or localposition that I didnt understand neither.