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)

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