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

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 :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();
}
}

What is going on there with the binary formatter AND the JSON?!!

Load/Save steps:

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

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

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() on a GameObject instance, or use ScriptableObject.CreateInstance() 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:

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.
Deserialization risks in use of BinaryFormatter and related types - .NET | Microsoft Learn

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

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