2
\$\begingroup\$

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?

\$\endgroup\$
14
  • 4
    \$\begingroup\$ What do you mean when you say it doesn't work? Is there an error or warning? Have you run a Debug.Log to see if the code is being reached? \$\endgroup\$
    – hobnob
    Jul 7, 2016 at 14:02
  • \$\begingroup\$ Yes sorry, I wasn't precise. The code is reached both with OnApplicationQuit() than with the Update() method and there are no error or warning. If I exit play mode in Unity and than hit play again, the data are saved only with OnApplicationQuit() and not if i save the within Update() \$\endgroup\$ Jul 7, 2016 at 14:32
  • \$\begingroup\$ Do you have the code for the Update? I'm assuming you're not calling it on every update cycle, as the performance hit on that would be large... are you using a co-routine, or just executing at specific times? \$\endgroup\$
    – hobnob
    Jul 7, 2016 at 14:35
  • \$\begingroup\$ I'm using it only if an event is triggered. void Update() { if (life < 20) { if (//certain event is triggered) { SaveLife (); } lifeTimer -= Time.deltaTime; if (lifeTimer <= 0) { ResetLife (); } } } \$\endgroup\$ Jul 7, 2016 at 14:47
  • 1
    \$\begingroup\$ Is the problem occuring when you check the file exists inside your code? Could it be that you're creating the file, but a file exists check fails because the internal cache hasn't caught up? \$\endgroup\$
    – hobnob
    Jul 7, 2016 at 15:48

1 Answer 1

1
\$\begingroup\$

The problem was caused by another SaveTime() method in the same script that was writing to the same file "playerInfo.dat".

public void SaveTime()
{
    BinaryFormatter bf = new BinaryFormatter ();
    FileStream file = File.Open (Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);
    PlayerData data = new PlayerData ();
    data.savedTime = DateTime.Now;
    bf.Serialize (file, data);
    file.Close ();
}

Since in SaveTime() I was instantiating again Player data, this seems to cause the old Player data created by Update() to be substituted.

I solved the problem by instantiating Player date only once in the main body of the script.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .