0

OnDisable and OnDestroy are not getting called on iOS. I have added these two methods in MonoBehaviour object which get destroyed when game is closed.

Which ever data I was storing at game close time that not become available at next game launch time. So,I realize that these two methods were not working.

Any solution for this?

void OnDisable()
 {
     Debug.Log("*************************** OnDisable");

     DataStorage.StoreLastOpenedDay(DateTime.Now.Day);
     DataStorage.StoreLastOpenedMonth(DateTime.Now.Month);
     DataStorage.StoreLastOpenedYear(DateTime.Now.Year);
 }


 void OnDestroy()
 {
     Debug.Log("*************************** OnDestroy");

     DataStorage.StoreLastOpenedDay(DateTime.Now.Day);
     DataStorage.StoreLastOpenedMonth(DateTime.Now.Month);
     DataStorage.StoreLastOpenedYear(DateTime.Now.Year);
 }

2 Answers 2

3

OnDisable and OnDestroy functions are called when the script is destroyed. They are not meant to be called when you are exiting out of the game. When you exit the game on mobile devices, the scripts are not usually destroyed. The game is simply paused. OnDisable and OnDestroy may work on standalone build when you exit app but that's not what it's used for and therefore may not work on mobile devices.

The OnApplicationPause and OnApplicationFocus functions are used to detect when you exit and return to your app. Use OnDisable and OnDestroy to only detect when the script or GameObject it is attached to is destroyed not when you press the home button or exit the app.

void OnApplicationPause(bool pauseStatus)
{
    if (pauseStatus)
    {
        Debug.Log("Paused");
    }
    else
    {
        Debug.Log("resumed");
    }
}

void OnApplicationFocus(bool hasFocus)
{
    Debug.Log("resumed");
}
1

You can try using OnApplicationQuit() instead of OnDestroy() and OnDisable() because this two methods are not getting called when you close your game, only when you actually get you object destroyed or disabled.

But if you want to use OnApplicationQuit() you should get in mind special platform setting called Exit on Suspend (on Unity under 2017) and Behaviour in Background (for Unity 2017.0 and newer). By default this setting is set to Suspend which means that when you click home button your application doesn't get destroyed, but only goes to background. To handle this situation you should use OnApplicationPause(). If you select Destroy behaviour then your app will be destroyed after clicking home button and OnApplicationQuit() will be called.

5
  • Read the doc about OnApplicationQuit. You must tick "Exit on Suspend" to cause the game to quit and not suspend for this function to be called. Unless OP is fine with app quitting instead of suspending this should not be used. Not to mention it doesn't work on Widows store apps.
    – Programmer
    Nov 10, 2017 at 10:39
  • At which place "Exit on Suspend" is available?
    – Siddharth
    Nov 10, 2017 at 11:04
  • @Programmer, there is no more tick "Exit On Suspend", in Unity 2017 it was moved to "Behaviour in Background", which is a dropdown menu with 3 items - Suspend, Custom, Exit. It's located in Player Settings under the Configuration header.
    – vmchar
    Nov 10, 2017 at 11:16
  • I know. I quoted the doc directly. My point is that you have to change the behavior of app being suspended to quit in order to use OnApplicationQuit which is bad for most apps and won't work on windows app and even on android sometimes. The OnApplicationPause I mentioned in my answer is highly recommended.
    – Programmer
    Nov 10, 2017 at 11:25
  • As per my requirements, I decided to use OnApplicationQuit() method because just one time I want to store the data. So marked this answer as correct one. But thank you so much for giving awesome replies.
    – Siddharth
    Nov 13, 2017 at 11:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.