Trouble Saving and Loading Serialization
Hello,
I am hoping someone would be able to help me, I have spent a while trying to serialise data within my project. The code is as follows:
void OnEnable()
{
if(File.Exists(Application.persistentDataPath + "/playerinfo.dat"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.persistentDataPath + "/playerinfo.dat", FileMode.Open);
PlayerData data = (PlayerData)bf.Deserialize(file);
Debug.Log(data.gold);
file.Close();
}
}
void OnDisable()
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerinfo.dat");
PlayerData data = new PlayerData();
data.gold = gold;
Debug.Log(data.gold);
data.goldPerClick = goldPerClick;
bf.Serialize(file, data);
file.Close();
}
[Serializable]
class PlayerData
{
public float gold;
public int goldPerClick;
}
}
As you can see from the above code i am looking at an automatic save and load system, I have added some Log commands and have noticed the data saves correctly but when loading returns to zero, I have no code within my game resetting these values.
I have also managed to find the incrypted data file so am certain its an issue with my load code, I would really appritiate it if anyone would be able to lend a noob a hand!
Your answer
Follow this Question
Related Questions
I am trying to make a dat file decoder to decode my game data but I can't figure it out please help 0 Answers
Save and Load data from a Scriptableobject 0 Answers
Saveing and Loading Problem 0 Answers
Save and load player position in main scene 1 Answer
How to load a score form a saved game vs. the level just completed 0 Answers