For some reason the game won’t load or maybe save whenever the game is opened or closed, I tried building the game and then running but it still has no effect. Here is the code:
void OnEnable()
{
Debug.Log("Enabled!!!");
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.persistentDataPath + "/playerInfo.dat");
PlayerData data = new PlayerData();
data.wood = wood;
data.woodperclick = woodperclick;
bf.Serialize(file, data);
file.Close();
}
public void OnDisable()
{
Debug.Log("Disabled!!!");
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);
file.Close();
wood = data.wood;
woodperclick = data.woodperclick;
}
}
}
[Serializable]
class PlayerData
{
public float wood = GameManager.wood;
public int woodperclick = GameManager.woodperclick;
}
Am I missing something? I watched the entire video tutorial for data persistence on the unity3d website multiple times.