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 /
  • Help Room /
avatar image
0
Question by Tsucasa · May 07, 2018 at 07:58 AM · scriptableobjectjsonsave datasaveloadscriptable object

jsonUtlility save/load ScriptableObjects

I'm currently working on a small project, this project contains one script with a template for Scriptable objects that contains 2 integers (questNumber and questStatus) I have created 5 objects from this template called quest1 - 5.

and a SaveLoad Script which uses the ToJson and FromJsonOverwrite methods (the content of these methods has been changing a lot as i have been trying all the other threads on this site)

I would like to save the Players progress using JsonUtility or any other method as I have been struggling to do this (I'm very new to serialization and games save/loads) and was hoping someone could give me a hand (I have been trying to crack this for 4 days now).

The plan I have is to save and load the state of all the Quest objects at runtime.

Thanks :)

Comment
Add comment · Show 3
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 Tsucasa · May 07, 2018 at 06:16 PM 0
Share

Ok so I have some more information in case it will help:

$$anonymous$$y save function is returning ' {}' in the json file, the code is:

     public static void Save()
     {
         var instance = Resources.LoadAll<Job$$anonymous$$anager>("");
         
         string data = JsonUtility.ToJson(instance);
         File.WriteAllText(Application.persistentDataPath + "/save.txt", data);
     }

I have all the objects created from the scriptableObject Named Job$$anonymous$$anager, however nothing is saved.

avatar image MacDx Tsucasa · May 07, 2018 at 09:37 PM 0
Share

It would be of great help if you could show the code for your Job$$anonymous$$anager. Some important things to remember, your class ($$anonymous$$onoBehaviour/ScriptableObject derived or plain class) should be marked with the serializable attribute. $$anonymous$$ake sure all the fields you want serialized are public and remember that the types supported are only those that Unity already serializes by default, so stuff like dictionaries or bidimensional arrays won't work.

avatar image Tsucasa MacDx · May 08, 2018 at 09:05 AM 0
Share

Hi $$anonymous$$acDx

Please see below the job manager: using System.Collections; using System.Collections.Generic; using UnityEngine;

 [CreateAsset$$anonymous$$enu(fileName = "Job", menuName = "Jobs")]
 [Serializable]
 public class Job$$anonymous$$anager : ScriptableObject {
 
     public int questNumber;
     public int questStatus;
     [NonSerialized]
     public bool created = false;
 
     void Awake()
     {
         if (!created)
         {
             DontDestroyOnLoad(this);
             created = true;
         }
     }
 }

I think i have serialized it correctly.

Thanks

1 Reply

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

Answer by MacDx · May 08, 2018 at 03:54 PM

@Tsucasa your JobManager class seems correct to me. The most likely thing to go wrong there then, is the loading of the JobManager object. Are you sure the instance variable isn't null when passing it to the ToJson method, put some logs there or debug with breakpoints so you can better know. Also instead of using LoadAll try using Load with the appropriate path.


Like this:

 //Assuming your file is named JobManager too and it is placed right below the resources folder
 var instance = Resources.Load<JobManager>("JobManager"); 

I tried that and it worked as expected. Hope this helps!

Comment
Add comment · Show 11 · 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 Tsucasa · May 09, 2018 at 11:02 AM 0
Share

Hi $$anonymous$$acDX

Here's a Screenshot of the 5 Objects created from the Job$$anonymous$$anager Script.

would var instance = Resources.LoadAll<jobmanager>("Jobs"); work?

Thanks

setup.png (5.5 kB)
avatar image Tsucasa · May 09, 2018 at 12:00 PM 0
Share

Hi $$anonymous$$acDx

I have tried var instance = Resources.LoadAll<jobmanager>("Jobs");

however when I debug.log it I just get jobmanager[] which if I then debug.log instance[0] I see job1. would a load work with that?

I tried this: foreach (var i in instance) { data += JsonUtility.ToJson(i); }

but the above doesn't create a valid Json file. ( see attached file)

Also please note i'm in the process of rena$$anonymous$$g Quest to Job hence why they say JobNumber and JobStatus rather than questNumber/Status as before.

Thanks

[1]: /storage/temp/116524-save.txt

avatar image MacDx Tsucasa · May 09, 2018 at 10:48 PM 0
Share

Assu$$anonymous$$g the data variable you're showing is an empty string, yeah that for loop will never work. ToJson returns a Json string that's supposed to be only one file but you are putting them all together then try to read them as a single file and that will not work

 //Your file would contain something like this
 {}{}{}{} //Invalid Json

Json files can only contain 1 root object (which can be an array), and values (being array elements or object properties) are always separated by commas, so yeah by mashing up multiple valid Json strings, output by the ToJson method, you are making a NON valid Json.

To make that code work you'll need to modify it a little bit like this:

 data = "[";
 for(int i = 0; i < instance.Length ; i++)
 {
     data += JsonUtility.ToJson(instance[i]);
     //Add a comma after every element except for the last one
     if( i < instance.Lenght - 1) data += ","; 
 }
 data += "]";
 //Then write data to a file with data looking like this
 // [{},{},{}] VALID JSON


avatar image Tsucasa MacDx · May 10, 2018 at 07:21 AM 0
Share

Hi $$anonymous$$acDx

Thanks the above did create a Valid Json file, Please can you confirm how I should load this data though? I can't think of how to assign each entry in the file to an instance of Job$$anonymous$$anager.

I was Currently using :

    if (FileCheck())
     {
         var instance = Resources.LoadAll<Job$$anonymous$$anager>("Jobs");
         var data = File.ReadAllText(Application.persistentDataPath + "/save.txt");

         JsonUtility.FromJsonOverwrite(data, instance);
     }
     else
     {
         Debug.LogError("Save file not found");
     }

However i believe that tries to over write one object with all the data?

Show more comments
avatar image Tsucasa · May 12, 2018 at 07:49 AM 0
Share

Hi $$anonymous$$acDx

Apologies for the lack of info, Basically when the save function is called it only seems to save the InsanceIds please see attached save.txt file.

when I do a Debug.Log, if I type just wrap I just get a Job$$anonymous$$anagerWrapper object, if I type wrap.array then I get Job$$anonymous$$anager[], if I type wrap.array[0] then I get Job1[job$$anonymous$$anager]

Please see attached screenshots for breakpoints results from the save.alt text (have to put the 2nd in a separate post as it wont allow me to add anymore attachments).

On Loading its passed all the instanceIDs that are in the save.txt, when I do a Debug.Log(j.array[0].jobStatus)) it's set to 0 and it should be 1 as in the save before that's what it was set to.

I hope this is enough info if there's anything else that may help you please let me know

Thanks

result1.png (39.5 kB)
save.txt (116 B)
avatar image Tsucasa Tsucasa · May 12, 2018 at 07:54 AM 0
Share

Here's screenshot 2

Also I just noticed if I Debug.Log(jsonData); in the Load function i get the same json data 5 times, see result3.png

[1]: /storage/temp/116798-result3.png

result2.png (42.1 kB)
result3.png (24.8 kB)
avatar image MacDx Tsucasa · May 13, 2018 at 10:23 PM 0
Share

I see. I wish to continue to help you but this is getting too long and it's a separate issue since saving/loading jsons is already working for you. The original question in this post has already been solved, so please ask a separate question.

Regards

Show more comments

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

140 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

Related Questions

Saving reference to a ScriptableObject with JsonUtility 1 Answer

Cannot deserialize JSON to new instances of type 'X' 1 Answer

[Error:] Cannot Deserialize JSON to new instances of type ' X ' 1 Answer

List to XML 0 Answers

How do I place a custom tile? 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