Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by cardician23 · Feb 16, 2019 at 10:14 PM · spriteserializationjson

Serialized objects have instanceID change on load.

So I'm seeing something happen and I want to know if someone can confirm is this is to be expected or if perhaps I'm screwing something up.

I've got an class that is a few strings and a number of Sprite objects. The class is serializable and I save it to a file using the JsonUtility class. Things look fine, though what ends up getting saved for the Sprites are instanceIDs. So one of the serialized objects will look like:


 [ { "objectName": "Test1", "spriteTop": { "instanceID": 5094 }, ...


This all works fine when loading the objects back in and everything. However, I've noticed that if I close Unity or restart my computer or something, next time I run things, all the sprites are off by one in their sprite sheet. The data in the Json file hasn't changed, the instanceIDs are still the same, but the actual sprites on the sprite sheets they point to have changed. Meaning if before the instanceID pointed to sprite 74 of a sprite sheet, now that ID will instead reference sprite 73. If that makes sense, not the easiest thing to explain.


So am I doing something wrong when serializing the sprites to a Json file? Or do I just need to create an alternate way of loading and serializing sprites instead of trying to serialize the actual sprite object?

Comment
Add comment · Show 1
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 zereda-games · Feb 17, 2019 at 08:05 AM 0
Share

make an 'int Id' and reference it somehow, ins$$anonymous$$d of the GetInstanceID method. Way i usually do it is i make a list of something i need a bunch of. In your case it's sprite, so a List""sprites; is what I'd use also a List""ids; then forloop in a method like i dunno Update works fine for testing, and make the two lists synchronize to one another, that way the ID of the Image is always the same. you can read and write that list entry into the JSON.

 for(int i=0;i<ids.Count;i++){
     ids[i]=i;
     sprites[i]=Resources.Load<Sprite>("SpritesFolder/defaultSprite.png");
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Feb 17, 2019 at 03:28 AM

Unity's instanceIDs are not GUIDs. They are not ensured to be globally unique. So yes, they can change between sessions. Unity uses them internally when serializing a scene for references to other objects within the same scene. Though for referencing assets which are stored seperately Unity uses the asset id which is an actual GUID. Unfortunately those are used internally by the assetdatabase and Unity doesn't really provide a proper API for accessing assetIDs are runtime.


So yes, you usually need your own way to serialize the information you need.

Comment
Add comment · Show 2 · 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 RobAnthem · Feb 17, 2019 at 08:20 AM 0
Share

I just did a bunch of tests, and apparently InstanceID's aren't even persistent through scene loading, even during an editor session. They are different for each clone, copy, or prefab instance, and never remain the same. They can be positive and negative numbers, and fetching the ID on a broken object throws errors about storing RA$$anonymous$$ allocations for over 4 frames. I always knew they were relatively useless but now I can't think of any good reason to use them.

avatar image DeveloperJake · Jan 18 at 01:32 AM 0
Share

So how do we fix this?

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

137 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

JsonUtility doesn't serialize nested mixed var 1 Answer

unity 3d loading data from json problem?! 1 Answer

Firebase Datasnapshot Json to Object using JsonUtility 1 Answer

MiniJSON Multiple Rows 1 Answer

JsonUtility serializes floats with way too many digits 2 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