Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to call StartCoroutine in OnApplicationQuit?

Discussion in 'iOS and tvOS' started by shinsugita, Jan 5, 2016.

  1. shinsugita

    shinsugita

    Joined:
    Sep 2, 2015
    Posts:
    5
    Hi there.

    Unity version is 5.1.2.

    I'd like to call method which return IEnumerator in OnApplicationQuit method.
    Because, I want to save game data which call Coroutine method when game was stopped.

    So, I call IEnumerator method in OnApplicationQuit. but it stopped.

    I tested it in UnityEditor.

    It is my snippet
    Code (CSharp):
    1.     IEnumerator SaveGame()
    2.     {
    3.         var i = 0;
    4.         while(true)
    5.         {
    6.             Debug.LogError(i);
    7.             if(i > 100)
    8.             {
    9.                 yield break;
    10.             }
    11.             i++;
    12.             yield return null;
    13.         }
    14.     }
    Code (CSharp):
    1.     void OnApplicationQuit(){
    2.         Debug.Log("OnApplicationQuit");
    3.         #if UNITY_IOS || UNITY_EDITOR
    4.         StartCoroutine(SaveGame());
    5.         #endif
    6.     }
    SaveGame method is test.
    I play UnityEditor and Stop. OnApplicationQuit was called.
    however, Debug.LogError(i) wrote down only once "0" log.

    I guess GameObject was destroyed when I stop UnityEditor?
    Could you tell me wait i variable became over 100?

    It is for iOS but I test in UnityEditor.I also develop for Android..
     
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Yes, your GameObjects are destroyed once you stop play mode.

    I don't think you get OnApplicationQuit called on iOS anyway. Perhaps you need OnApplicationPause or OnApplicationFocus? These will be called when you send the app to the background, although note that your coroutine will not run while the app is in the background - you'll get the first frame only, but it will continue once the app is restored.
     
    shinsugita likes this.
  3. CPXTom

    CPXTom

    Joined:
    Apr 24, 2010
    Posts:
    113
    Last time I checked OnApplicationQuit is not called on iOS. You also shouldn't rely on the app closing to save data, as the app can close at any time. You could use OnApplicationPause as @andymads suggested, but this won't catch the case if your app crashes (due to memory or an uncaught exception).
     
    shinsugita likes this.
  4. shinsugita

    shinsugita

    Joined:
    Sep 2, 2015
    Posts:
    5
    Thank you for replying.
    I called like this. it worked good in UnityEditor.
    Code (CSharp):
    1. var e = SaveGame();
    2. while (e.MoveNext()){}
     
  5. shinsugita

    shinsugita

    Joined:
    Sep 2, 2015
    Posts:
    5
    Thank you for replying.
    But, iOS can called OnApplicationQuit method in Unity 5.0.2f1 before. What version do you use?

    However Android could not call OnApplicationQuit method so, I wrote my code in OnApplicationPause

    Code (CSharp):
    1. void OnApplicationPause(boolpauseStatus){
    2.     if(pauseStatus){
    3.         // some saving code.
    4.     }
    5. }