Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

[SOLVED] OnApplicationPause() with a save function does not work.

Discussion in 'Scripting' started by Etaodev, Dec 26, 2019.

  1. Etaodev

    Etaodev

    Joined:
    Dec 8, 2019
    Posts:
    13
    Hello,
    So I am developing this game for the android platform, and upon testing my apk I realised that the save and load functionality that worked like a charm in the editor didn't work. Initially, I thought it was because I used OnDestroy() for the saving, so I switched to OnApplicationPause(). Only to realise that it doesn't work either.

    I have my load function in the "public void Start()" method. And save function in the "public void OnApplicationPause()" method. Is there something wrong with this? This is definitely a matter of calling these save and load functions cuz it executed flawlessly in the Editor.

    Please help ;-;
     
  2. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,588
    Where do these save/load methods write/read from? I mean, where are you trying to save the data?

    Also be aware that functions like OnApplicationPause and OnApplicationQuit may have different behaviour on the various target platforms, as outlined in their documentation, because some platforms treat application sessions very differently.

    I would recommend to attach the debugger and check what's executed at all, then dig deeper...
     
  3. Etaodev

    Etaodev

    Joined:
    Dec 8, 2019
    Posts:
    13
    Hello, thank you for replying. They read and write from "Application.dataPath". And could you please elaborate on the debugger thing. and advice me on what to do next? Thankyou!
     
  4. Suddoha

    Suddoha

    Joined:
    Nov 9, 2013
    Posts:
    2,588
    First try not to write to the data path. Instead, use Application.persistentDataPath.

    As for the debugger, it's best to look that up in the docs for the specific platforms.
    It's really easy on the Editor and in standalones, because most IDEs have that functionality built-in, but if you wanna debug a deployed app, you might need to configure / pair any other device first.
     
  5. Etaodev

    Etaodev

    Joined:
    Dec 8, 2019
    Posts:
    13
    Hey, thank you for your help! I fixed this issue by switch to using the OnApplicationFocus() method, because according to the official documentations, this method triggers every time the application loses focus. But, this wasn't the cause of the issue. Turns out I never even passed in the needed boolean parameter for OnApplicationPause() to work, thats why it didn't work. As I said, I switch to using the OnApplicationFocus() method as it was more reliable.

    Code (CSharp):
    1. public void OnApplicationFocus(bool focusing){
    2.      //if program is not in focus, save
    3.      if(!focusin){
    4.      SaveBySerialisation();
    5.      }
    6. }
    Thank you for putting your time into fixing my issue!
     
unityunity