Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question i need help... i try to load my game that i saved but i get a error.

Discussion in 'Scripting' started by justinsommermeyer41, May 26, 2023.

  1. justinsommermeyer41

    justinsommermeyer41

    Joined:
    Apr 16, 2023
    Posts:
    3
    Hello, i need help.

    I try to read a json file that i create when i save my Game, but i get a error everytime, i looked over it myself and even looked online but i cant find the problem. i would be very gatefull when someone could help me.

    Debug output:

    ArgumentException: JSON parse error: Invalid value.
    UnityEngine.JsonUtility.FromJsonOverwrite (System.String json, System.Object objectToOverwrite) (at <e000c5ade9084c9c844e11c13b6c7613>:0)

    Method (Error in 46)

    public void Load()
    {
    if(File.Exists(string.Concat(Application.persistentDataPath, savePath)))

    {
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Open(string.Concat(Application.persistentDataPath, savePath), FileMode.Open);
    Debug.Log("2");
    46 JsonUtility.FromJsonOverwrite(bf.Deserialize(file).ToString(), this);
    Debug.Log("3");
    file.Close();
    }
    }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    What is going on there with the binary formatter AND the JSON?!!

    Load/Save steps:

    https://forum.unity.com/threads/save-system-questions.930366/#post-6087384

    An excellent discussion of loading/saving in Unity3D by Xarbrough:

    https://forum.unity.com/threads/save-system.1232301/#post-7872586

    Loading/Saving ScriptableObjects by a proxy identifier such as name:

    https://forum.unity.com/threads/use...lds-in-editor-and-build.1327059/#post-8394573

    When loading, you can never re-create a MonoBehaviour or ScriptableObject instance directly from JSON. The reason is they are hybrid C# and native engine objects, and when the JSON package calls
    new
    to make one, it cannot make the native engine portion of the object.

    Instead you must first create the MonoBehaviour using AddComponent<T>() on a GameObject instance, or use ScriptableObject.CreateInstance<T>() to make your SO, then use the appropriate JSON "populate object" call to fill in its public fields.

    If you want to use PlayerPrefs to save your game, it's always better to use a JSON-based wrapper such as this one I forked from a fellow named Brett M Johnson on github:

    https://gist.github.com/kurtdekker/7db0500da01c3eb2a7ac8040198ce7f6

    Do not use the binary formatter/serializer: it is insecure, it cannot be made secure, and it makes debugging very difficult, plus it actually will NOT prevent people from modifying your save data on their computers.

    https://docs.microsoft.com/en-us/dotnet/standard/serialization/binaryformatter-security-guide
     
  3. justinsommermeyer41

    justinsommermeyer41

    Joined:
    Apr 16, 2023
    Posts:
    3
    oh okay, i look over it and try it thank you. saw it on YT like this and thought it would work like in the video lmao
     
  4. justinsommermeyer41

    justinsommermeyer41

    Joined:
    Apr 16, 2023
    Posts:
    3
    public void Save()
    {
    string saveData = JsonUtility.ToJson(this, true);
    BinaryFormatter bf = new BinaryFormatter();
    FileStream file = File.Create(string.Concat(Application.persistentDataPath, savePath));
    bf.Serialize(file, savePath);
    file.Close();
    }


    Thats my Save Method, all fine with it and it workes mhm