Good afternoon and please forgive, but ... What the hell am I doing wrong?
I try to implement a simple project with different save options (JSON, PlayerPrefs, JSON + PlayerPrefs), but from the very beginning everything goes wrong.
Why is the JSON file empty? If it should be, then the download does not work

Saveloader

using System.Collections; using System.Collections.Generic; using System.IO; using UnityEngine; public class SaveLoad : MonoBehaviour { private Model model; private string path; void Start () { path = Path.Combine(Application.dataPath, "Save.json"); } private void Update() { if (Input.GetKeyDown(KeyCode.F5)) JSONsave(); else if (Input.GetKeyDown(KeyCode.F6)) JSONload(); } private void JSONsave() { model = GetComponent<Model>(); Debug.Log("HP saver object - " + model.HP); Debug.Log("Saver path - " + path); var JSON = JsonUtility.ToJson(model, true); Debug.Log("Saver string - " + JSON); File.WriteAllText(path, JSON); } private void JSONload() { model = JsonUtility.FromJson<Model>(File.ReadAllText(path)); Debug.Log("HP load - " + model.HP); } } 

The script that should be preserved

 using System; using System.Collections; using System.Collections.Generic; using UnityEngine; [Serializable] public class Model : MonoBehaviour { [SerializeField] public int HP { get; set; } void Start () { HP = 3; } } 

ArgumentException: Cannot deserialize JSON to new instances of type 'Model.' UnityEngine.JsonUtility.FromJson (System.String json, System.Type type) (at C: /buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs: 48) UnityEngine.JsonUtility.FromJson [Model] ( System.String json) (at C: /buildslave/unity/build/Modules/JSONSerialize/Public/JsonUtility.bindings.cs: 33) SaveLoad.JSONload () (at Assets / Scripts / SaveLoad.cs: 36) SaveLoad.Update () (at Assets / Scripts / SaveLoad.cs: 21)

enter image description here

  • And where is JSON itself, and where is the data from File.ReadAllText(path) ? We wonder why you can not do it all? And also please, no, I beg you to transfer all the code from the pictures and write it with the text! - EvgeniyZ 4:27 pm
  • Would show how to save ... 90% of the cant there - Ljil
  • @EvgeniyZ I apologize, I use this resource a second time and for the first time I show the code here (after all, I am still too young and inexperienced) ... Usually everything was somehow in Google, but I don’t even know something here ... Yes I know that I am not very smart, but please, be patient :) - Matrzaei

1 answer 1

You do everything right with saving in json, but the problem lies in the fact that you cannot serialize properties. This is indicated in the documentation for SerializeField .

That is, if you do this:

 [Serializable] public class Model : MonoBehaviour { [SerializeField] private int _hp; public int HP { get { return _hp;} set { _hp = value;} } void Start () { HP = 3; } 

then everything should work fine. To avoid similar problems in the future, check the serialization in the inspector. That is, JsonUtility will serialize the class just as you will see it in the Inspector window. And if you do not want the serializable private field to be visible in the inspector, use the [HideInInspector] attribute

  • You have no idea how grateful I am to you! Because of my laziness, inattention and unwillingness to climb into the documentation, I already managed to fill up the TZ with this (although there was still FRP which I also don’t know, but then I stopped at Serialization ... Thanks again :) - Matrzaei
  • I am glad to help. The devil is in the details) - vmchar