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 14, 2018 at 07:14 AM · arrayscriptableobjectloadingjson

Unable to load Json to an object array

Basically when the save function is called it only seems to save the InsanceIds to the json file please see attached save.txt file (maybe on a separate post due to upload limit). alt text when I do a Debug.Log, if I type just wrap I just get a JobManagerWrapper object, if I type wrap.array then I get JobManager[], if I type wrap.array[0] then I get Job1[jobManager]

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.

 public static void Save()
     {
         JobManagerWrapper wrap = new JobManagerWrapper();
         wrap.array = Resources.LoadAll<JobManager>("Jobs");
         string jsonData = JsonUtility.ToJson(wrap);
 
         File.WriteAllText(Application.persistentDataPath + "/save.txt", jsonData);
     }
 
     public static void Load()
     {
         if (FileCheck())
         {
             string jsonData = File.ReadAllText(Application.persistentDataPath + "/save.txt");
             JobManagerWrapper j = JsonUtility.FromJson<JobManagerWrapper>(jsonData);
             Debug.Log(jsonData);
         }
         else
         {
             Debug.LogError("Save file not found");
         }
     }
 
 public class JobManagerWrapper
 {
     public JobManager[] array;
 }

Thanks

result1.png (39.5 kB)
result2.png (42.1 kB)
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 Tsucasa · May 14, 2018 at 07:15 AM 0
Share

here are the other files

alt text

save.txt (116 B)
result3.png (24.8 kB)
avatar image MacDx · May 14, 2018 at 04:23 PM 0
Share

So the problem is that the Job$$anonymous$$anager class is not being properly serialized since the Json file should contain other properties besides instanceID, like jobStatus. Correct? Could you also please post your current implementation of the Job$$anonymous$$anager class?

avatar image Tsucasa MacDx · May 14, 2018 at 07:06 PM 0
Share

Well that's what i thought if you drill down enough you get the details the example would be Debug.Log(j.array[0].jobStatus)) does give you the information.

Here's the Job $$anonymous$$anager code

 [CreateAsset$$anonymous$$enu(fileName = "Job", menuName = "Jobs")]
 [Serializable]
 public class Job$$anonymous$$anager : ScriptableObject {
 
     public int jobNumber;
     public int jobStatus;
 
     void Awake()
     {
             SaveLoad$$anonymous$$anager.Load();
     }
 
 }
avatar image Tsucasa Tsucasa · May 18, 2018 at 06:06 PM 0
Share

Hi $$anonymous$$acDx

Is there any further information I can provide you? I have been unsuccessful in getting this working

Thanks

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by MacDx · May 18, 2018 at 10:06 PM

Ok after testing and researching this topic myself it looks like the problem is Unity's serialization ways, when you serialize a single object it gives the correct output because it has to look into the contents of the object. However, when you pass it an object with an array of references, like in your case, Unity says, these are references to Objects (Unity's own object type that contains instanceID) so it will be better if I save the ID from this instead since you can fetch it by ID later (this is my guess).


So the options I can come up with for you are:


1) Once again, switch to Json .NET or any other json library, just stop using JsonUtility.


2) Create a proxy object which is a plain C# class that has the same properties as the scriptable object and change the wrapper class to use those instead in the array. Then have a method on your scriptable object that returns an fresh instance of the proxy type with it's properties already set, so when you get the array of JobManagers using Resources.LoadAll you can call that method and populate the wrapper's array more easily.


Regards

Note: Also stop calling Load in the scriptable object's Awake method, it is a bad idea to do so. Use a button or some monobehaviour's awake/start instead.

Comment
Add comment · Show 3 · 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 · Jun 01, 2018 at 05:22 PM 0
Share

Hi $$anonymous$$acDx

Sorry for the late response I've been really busy, can you confirm what you mean by a Proxy Object? or show me how its implemented.

I will not load from the Awake() as I said it was a temporary measure.

Thanks

avatar image Tsucasa Tsucasa · Jun 02, 2018 at 08:27 AM 0
Share

Hi $$anonymous$$acDx

I have been Debugging the code further and found there is an error which is why its not working, here's the error:

"ArgumentException: JSON must represent an object type. UnityEngine.JsonUtility.FromJson[Job$$anonymous$$anagerWrapper] (System.String json) (at C:/buildslave/unity/build/artifacts/generated/common/modules/JSONSerialize/JsonUtilityBindings.gen.cs:25) SaveLoad$$anonymous$$anager.Load () (at Assets/Scripts/SaveLoad$$anonymous$$anager.cs:63) JobSearch.Start () (at Assets/Scripts/JobSearch.cs:24)"

the above seems to happen when this line of code executes

 Job$$anonymous$$anagerWrapper j = JsonUtility.FromJson<Job$$anonymous$$anagerWrapper>(jsonData);

hope this helps

avatar image Tsucasa Tsucasa · Jun 02, 2018 at 10:09 AM 0
Share

Hi $$anonymous$$acDx

I have been reading further online and this is my code so far however Jobs is always set to null.

         public static Job$$anonymous$$anager[] Load()
         {
             if (FileCheck())
             {
                 string jsonData = File.ReadAllText(Application.persistentDataPath + "/save.txt");
                 Debug.Log(JsonHelper.FromJson<Job$$anonymous$$anager>(jsonData));
                 Job$$anonymous$$anager[] jobs = JsonHelper.FromJson<Job$$anonymous$$anager>(jsonData);
                 // Job$$anonymous$$anagerWrapper j = JsonUtility.FromJson<Job$$anonymous$$anagerWrapper>(jsonData);
                 return jobs;
             }
             else
             {
                 Debug.LogError("Save file not found");
                 return null;
             }  
         }
     }
     
     public static class JsonHelper
     {
         public static T[] FromJson<T>(string json)
         {
             Wrapper<T> wrapper = JsonUtility.FromJson<Wrapper<T>>(json);
             return wrapper.Items;
         }
     
         [Serializable]
         private class Wrapper<T>
         {
             public T[] Items;
         }
     }

This was my source of information here.

Thanks and apologies for the post overload I just really want to get this sorted.

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

155 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

Related Questions

Calling an array 1 Answer

JSON weird deserialization 1 Answer

Storing levels data as json? 0 Answers

Serialization of ScriptableObject not hexadecimal (json) 1 Answer

How to reliably send Kinect v2 data over MQTT? 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