Home>

I am a beginner making games with Unity.
I am making a function to save, read and write json format data
The reading part just doesn't work.
Specifically,
-The file has been written.
・ It will be initialized when reading, and will not be in the expected state.
I found the location of the file using Application.persistentDataPath and checked it. If i check after the program is finished, it will contain the changed data, but the next time you start the program, the contents of the file will be initialized and the original data will be lost.

public void SavePlayerData (Player player)
{
StreamWriter writer;
FileStream fs = File.OpenWrite (Application.persistentDataPath + "/savedata.json");

string jsonstr = JsonUtility.ToJson (player);
writer = new StreamWriter (fs);
writer.Write (jsonstr);
writer.Flush ();
writer.Close ();
}
public Player loadPlayerData ()
{
string datastr = "";
FileStream fs = File.OpenRead (Application.persistentDataPath + "/savedata.json");
StreamReader reader;
reader = new StreamReader (fs, Encoding.UTF8);
datastr = reader.ReadToEnd ();
reader.Close ();
return JsonUtility.FromJson(datastr);
}

I would be pleased if anyone could tell me how I could read the stored data without initializing it.