- Home /
When does the "InstanceID" get assigned, and does it seralize with the Object?
I have a couple of related questions about the instance id in UnityEngine.Object
(that you could get via GetInstanceID
)
When does this ID get assigned? (I assume it's when the object is instantiated)
Does it get serialized with the object? (i.e. if I have an object, and an assembly reload occurs, would my object still have its old instance id? or a new one?) (to put this question in other words: does the id persist when I exit/enter Unity? or when I load another scene and re-load the previous scene where the object was at?)
One might say, well why don't you try and see for yourself? Well, I did try and print the instance id of an object: first time I did it I got a value, I restarted unity and got a new one, I restarted again, but this time the value persisted... so I couldn't exactly tell what's going on...
Thanks!
EDIT:
OK so here's a quick test I just did:
I turned on debug mode on of my objects, first, the instance ID was 9390. I restarted Unity and it persisted. However, I reloaded the scene that I was in, it changed to something in the ten thousands. I reloaded the scene again, the value is back to 9390!
Since instanceId belongs to Object it would have to be created there.
As for the instanceID value, it does not seem to change for me. Running with Debug mode on the inspector, my instanceID remains the same even if I shut down and restart Unity.
Thanks. Have you tried reloading your current scene? does it still persist?
What do you mean by reloading? By code? Like
Application.LoadLevel(Application.loadedLevel);
One thing though, what I see is the instanceID of Component but I guess it works the same with GameObject.
yeah well, since Components are Objects, they will also have their IDs. By reloading I just meant, if you're in scene X, go to scene Y, and then get back to X.
Ok no it actually increases each time. This is expected since a new object is created each time and the instanceID has to be unique so it cannot be persistent.
Answer by fafase · Mar 01, 2014 at 10:56 PM
Ok me again, I have two scenes with the same setup, when I start the game I get a value on scene one, when I go back and forth between first and second scene, I get increasing values, I never get back to original until I stop the game, then I get the original ones. This is what I consider an expected behaviour.
The original values on scene one are given based on the objects loaded in the scene, in my case they all start at around 7300. If I switch to scene two without running, I get the same values, since I have the same setup though they are different objects in different scenes.
Conclusion: Unity starts from a defined value, then increases that value when new objects are showing up but never gets back. Since the value is unique, there cannot be the same value used twice, not even for a similar object to a reloaded of a scene.
When you run the same application twice, the starting objects will have the same values since the starting setup is the same, run the game the exact same way twice and it is all identical, shoot one more bullet in scene one and all should get moved one index up.
Answer by schkolne · May 15, 2015 at 08:18 AM
My understanding has always been that the InstanceID is the base memory address of the object itself. This is just a hypothesis.. I haven't seen it in the docs, but it is consistent with the behavior I've seen. In regular C++ you can always, at any time, query and print the pointer for an object, and that's what seems to be happening with InstanceIDs.
The idea is that, each time Unity runs, it allocates the objects as needed and puts them in an open spot in memory. Where happens to be open, it goes. So, sometimes, it will end up in the same spot on repeat runs. Of course, this can't be trusted, as the memory conditions can vary (especially when we hop between editor and running a build). However it is a very useful way of seeing, for example, if two separate variables are actually pointing at the same object.
The docs say "The instance id of an object is always guaranteed to be unique" and that's all they say. Use the InstanceID for a single debug session, to do some deep bookkeeping to figure out funny behavior. Don't rely on it for anything more than this. If you need a unique identifier to be serialized with an object, create it and manage it yourself. Expect InstanceID to vary wildly and unpredictably each time you re-run your program.
Your answer
Follow this Question
Related Questions
Getting instance ID from position 1 Answer
InstanceIDtoObject namespace? 1 Answer
Custom Editor serializing data in prefab instance. 0 Answers
giving serialized objects their own ids 0 Answers