Does Object.GetInstanceID value ever change at runtime?

Title says it all. Can it change, or will it be constant for each object while the game is running?

Imagine object A has GetInstanceID = 123 at game start. At time > 0, is it possible that GetInstanceID for the very same object will be different than 123?

Documentation only says it’s unique, but does not say anything about being constant at runtime.

1 Like

No, it doesn’t change during runtime. However, it is not serialized either.

1 Like

So it can change from one run to another? (for the same object, which did not get deleted, of course)

If when the object gets instantiated on the editor, i save it’s instance id on a serialized variable on a script attached to the same object, is that trouble?

No, you shouldn’t change that value as Unity uses that to track the object.

If you still want to use this type of system, you’ll need to implement your own “wrapper”, however this could get quite messy. Are you sure you need this type of solution?

Anyways, you’d just need to basically implement a system similar to pointer handles. Each object that needs a static GUID, would need a script that serilizes its first ran GUID, and then checks in a hash table if the value collided with a now-not-so-unique-GUID from another object that has a static GUID. If it doesn’t, just store that as this objects identifier, and each run you map the original(static) ID to the current ID whenever you need to interface with Unity’s version of the GUID. If it does, you need to use some form of collision resolution, the most basic would be incriminating the value until it’s open, but this tends to bubble up the values and can be a big issue if you have a lot of values.

Well, i’m not really changing the value, i just store it at a variable myself to detect duplicates.

What i do is :

  1. During the editor, when a specific button is pressed, i do some processing with all scene objects and save the processing done to a script attached to each object. Each object’s instance id is stored on a instanceID variable.
  2. During the editor, if an object that was previously processed gets duplicated (Ctrl+D or Ctrl +C + Ctrl + V), the object receives a new instance id, but their script was copied and has the data from the original object (so they have the instanceID from the other object)
  3. When the game starts, i check if the stored instanceID variable is equal to the object.GetInstanceID to see if the object is a duplicate, if it is, i calculate it’s data, because what it currently has is data from the original object it copied from.

Is this process i’m doing reliable?

No. They will not persist past game/editor instance loads nor scene changes.

I see. Thanks!

1 Like

Still this is the best thing for detecting duplication of complicated objects. Unfortunately instance id is not serialized but it’s not that bad to always reinitialize objects after scene load.

Dude… you keep necroing posts about GetInstanceID… what is up with that?

1 Like