Application.PersistentDataPath not working On Android

Hello,

I want to save 2 integer variable (coins and money) in my game. I search the web about save and load and learn i have two options PlayerPrefs and Persistence Data.
I have watched this video and wrote the same script. Persistence: Saving and Loading Data - Unity Learn
The only difference i have made is i didn’t used buttons, i used load function on Start and save on ApplicationQuit (I want to make Auto Save)
By the way, when i used the buttons (save and load buttons) it’s working on phone…

When i wrote the same script like in the video with buttons save and load system works good on pc and on phone but when i used on start and on applicationquit script (the only difference like i sad above is this part and no buttons) the script works on pc but it doesn’t work on Android phone.

void Start()
{
Load();
}

public void Save()
{

BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create(Application.persistentDataPath + “/playerinfo.dat”);
PlayerData data = new PlayerData ();
data.x = LoadCoin;
data.y = LoadMoney;
bf.Serialize (file, data);
file.Close ();

}

public void Load()

{
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);
LoadCoin = data.x;
LoadMoney = data.y;
file.Close ();

}

void OnApplicationQuit()
{
Save ();
}

Note: I choosed both write permission (External (Sd Card) or Internel), doesn’t work on android phone…

Thanks…

I would guess that OnApplicationQuit isn’t being called for some reason.

Really i don’t understand why it does not working… In the video, programmer wrote onGUI function another script (adjust script) and called GameControl.control.Load(); and GameControl.control.save();… I tried this version but the same result, not working…

OK, but the buttons worked, so I would guess it’s OnApplicationQuit that isn’t being called. What about trying OnDisable? Or just trying a key input to see if it saves that way as an experiment.

OnApplicationQuit is not guaranteed to be called on Android because of the design of how android disposes it’s Activity. If the data is crucial save it during major game state changes* like when they buy something or they hit a checkpoint.

If you are not able to do this for some reason, I’d suggest make a infinite-loop Coroutine that checks every 2-5 seconds the hash of your data. If it has changed from the last check Save if not do nothing. It’s like ‘Auto Save’ but simplified.

I tried OnDisable but the result was same…

You right OnApplicationQuit didn’t work… I tried your advice “Coroutine” it was worked, thanks. :slight_smile:

I use OnApplicationPause and it works great. BUT for some reason the saves deletes itself after 24 hours…

Why don’t you just let it save when anything like scores etc. changed?

1 Like