I'm trying to save player life within a binary file in Application.persistenceDataPath. I've written a method to store the player life:
public void SaveLife()
{
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
PlayerData data = new PlayerData ();
data.savedLife = life;
bf.Serialize (file, data);
file.Close ();
}
The problem is that when the SaveLife() is called OnApplicationQuit it works. Instead, if I call it within an Update() method of a class, it doesn't work.
The Update() method is called when a certain event is triggered:
void Update()
{
if (life < 20)
{
if (//certain event is triggered)
{
SaveLife();
}
lifeTimer -= Time.deltaTime;
if (lifeTimer <= 0)
{
ResetLife ();
}
}
}
The container class for the data is
[Serializable]
class PlayerData
{
public int savedLife;
}
What could be the problem?
void Update() { if (life < 20) { if (//certain event is triggered) { SaveLife (); } lifeTimer -= Time.deltaTime; if (lifeTimer <= 0) { ResetLife (); } } }
\$\endgroup\$