I can't get the correct stored values.
During game mode everything is fine-I can load and save without a problem, but when restart the game it's all gone.
An interesting thing is that If I try to view the es3 file using a notepad after I stop the game, and restart the game, then there's no problem
Code: Select all
private void Awake()
{
if (instance == null)
{
instance = this;
file = new ES3File("myFile.es3");
scores= LoadFromFileES3("scores", -1));
Debug.Log("scores= "+ scores);
}
else if (instance != this)
{
Destroy(gameObject);
}
}
void OnApplicationPause(bool boo)
{
file.Sync();
}
public void SaveToFile<T>(string key, T value)
{
file.Save<T>(key, value);
}
public T LoadFromFileES3<T>(string key, T defaultValue)
{
T valueToReturn= defaultValue;
if (!file.KeyExists(key))
{
Debug.Log(key + " tag doesn't exist");
SaveToFile(key, defaultValue);
}
else
{
Debug.Log(key + "tag exists");
valueToReturn= file.Load<T>(key, defaultValue);
Debug.Log("value stored in file or default= "+valueToReturn);
}
}
void SaveScores(){
Debug.Log("updating scores");
SaveToFile("scores",1000);
scores= LoadFromFileES3("scores", -1));
Debug.Log("playmode: scores= " +scores);}
}
scores tag doesn't exists
scores=-1
updating scores
scores tag exists
value stored in file or default=1000
playmode: scores=1000
Output second time (file exists):
scores tag exists
value stored in file or default=-1
scores=-1
Why doesn't it get the correct value? scores=1000 on second launch? Why does it return a default value