Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
3
Question by Garrafote · Nov 03, 2016 at 04:13 PM · c#serializationscriptableobjectjson

How to serialize a reference to a ScriptableObject asset file using the JsonSerializer

Hi, I'm trying to serialize a ScriptableObject that references several arrays of other ScriptableObject classes using the builtin JSON Serialization. This serialization needs to be done at runtime and all the referenced ScriptableObjects exist as actual assets in my project folder.

here is the code from the class i'm trying to serialize so far:

 public class Inventory : ScriptableObject {
 
     [Header("Upgrades")]
     public Collector[] collectors;
     public Fin[] fins;
     public Turbine[] turbines;
 }

 public class Upgrade : ScriptableObject {
 
 }
 
 [CreateAssetMenu]
 public class Collector : Upgrade {
 
     [Header("Collector Modifiers")]
     [Range(0.5f, 2f)]
     public float maxCargo = 1;
 
     [Range(0.5f, 2f)]
     public float collectRadius = 1;
 
     [Range(0.5f, 2f)]
     public float recyclingBonus = 1;
 }

and this is the output that i get when I serialize the Inventory class:

 var json = JsonUtility.ToJson(inventory);

 {
     "collectors": [
         {
             "instanceID": 12822
         },
         {
             "instanceID": 11936
         },
         {
             "instanceID": 10726
         }
     ],
     "fins": [
         {
             "instanceID": 10636
         },
         {
             "instanceID": 10802
         }
     ],
     "turbines": [
         {
             "instanceID": 10364
         },
         {
             "instanceID": 11142
         }
     ]
 }

Which is sort of what I want. I want the json to store only the references of the ScriptableObjects. And this is kind of what I'm getting here but my problem is that these InstanceIDs will have changed after unity is restarted, and, in consequence of that, I will lose all my references on the next play session.

I understand that ScriptableObjects dont necessarily need to be associated with an actual asset file but in this case I can assure they will. What I want is an easy way to store and retrieve a reliable reference to these assets at runtime.

Have anyone came across a similar situation? Any suggestion on how to approach this problem?

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by TonyLi · Dec 13, 2016 at 06:36 PM

According to the release notes, Unity 5.5.0 now serializes references by persistent fileID, not by ephemeral instanceID as in previous versions.

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 $$anonymous$$ · Jun 26, 2017 at 03:42 PM

That only works in the editor. Is there a way to do this at run time too?

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 TonyLi · Jun 26, 2017 at 04:15 PM 0
Share

I haven't found a release version of Unity that does this correctly. (Either that or I'm doing something wrong.) You'll probably have to maintain your own unique referencing system.

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

237 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 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

Serialize a list of scriptable objects to Json 0 Answers

ScriptableObject error with Inheritance 2 Answers

ScriptableObjects in a List all become nulls on Play 0 Answers

Distribute terrain in zones 3 Answers

Cannot serialize System.Type field 3 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