Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
4
Question by vexe · Mar 01, 2014 at 11:16 AM · serializationinstanceidunityengine.object

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)

  1. When does this ID get assigned? (I assume it's when the object is instantiated)

  2. 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!

Comment
Add comment · Show 9
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Mar 01, 2014 at 11:32 AM 0
Share

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.

avatar image vexe · Mar 01, 2014 at 11:36 AM 0
Share

Thanks. Have you tried reloading your current scene? does it still persist?

avatar image fafase · Mar 01, 2014 at 11:50 AM 0
Share

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.

avatar image vexe · Mar 01, 2014 at 11:52 AM 0
Share

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.

avatar image fafase · Mar 01, 2014 at 12:00 PM 0
Share

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.

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
1

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UnityEngine.Object field pointing to Project file gets serialized by JSON as instance ID. After deserialization field points to different Object. Why? 1 Answer

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges