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 /
  • Help Room /
avatar image
2
Question by $$anonymous$$ · Nov 29, 2016 at 12:07 PM · scriptableobjectjsonscriptable object

Saving reference to a ScriptableObject with JsonUtility

I have a data structure like the following

     [Serializable]
     public class InventoryWrapper
     {
         public CardInventory[] Data = new CardInventory[0];
     }
     [Serializable]
     public class CardInventory
     {
         public CardDefinition Definition;
         public int Quantity;
     }

CardDefinition in the example above is a ScriptableObject class. I'm using the JsonUtility.ToJson and JsonUtility.FromJson functions to save this data structure to disk and loading later. It appeared to work fine and generates output as follows:

 {
    "Data":[
       {
          "Definition":{
             "instanceID":377496
          },
          "Quantity":2
       },
       {
          "Definition":{
             "instanceID":377502
          },
          "Quantity":2
       },
       {
          "Definition":{
             "instanceID":-2897598
          },
          "Quantity":2
       }
    ]
 }

This morning all my references broke and when the data is serialised FROM JSON I get null objects instead of references to my ScriptableObjects. Am I going about this in the wrong way? Can anyone help

Comment
Add comment · Show 6
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 kashif619 · Jun 14, 2017 at 12:35 AM 0
Share

Is it just me or your last instanceID seems weird (Is negative -2897598).

avatar image Deadcow_ · Nov 21, 2017 at 04:48 AM 0
Share

Did you managed to fix this? I just encountered same problem

avatar image $$anonymous$$ · Nov 21, 2017 at 08:58 AM 0
Share

No. I ended up referencing them by name and hoping the filenames never change.

avatar image haruna9x · Nov 21, 2017 at 09:55 AM 0
Share

instanceID is unique for each session of Unity. It will be changed when you restart Unity.

avatar image $$anonymous$$ · Nov 21, 2017 at 09:57 AM 1
Share

Why would Unitys serialisation save an ID that changes in every session? That makes no sense.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Deadcow_ · Nov 21, 2017 at 04:01 PM

Ok, that was easy. To write GUID instead of InstanceId (i.e. preserve object references) we need to use EditorJsonUtility instead of JsonUtility

Comment
Add comment · Show 1 · 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 Prosto_Lyubo · Jan 26 at 06:52 PM 0
Share

EditorJsonUtility exists only in the UnityEditor namespace. There's no use for it in a game.

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

81 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

Related Questions

jsonUtlility save/load ScriptableObjects 1 Answer

Abillity system for turn based strategy 0 Answers

Custom inspector - How to serialize scene objects in ScriptableObject? 0 Answers

Why "the associated script can not be loaded" for some Scriptable Objects? 1 Answer

Serialization issue after updating Unity from 2018.2.1 to 2019.2.6 1 Answer


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