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 ();
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.