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. Dismiss Notice

Application.wantsToQuit and Application.quitting causing crash

Discussion in 'Scripting' started by Lyraedan, Apr 12, 2021.

  1. Lyraedan

    Lyraedan

    Joined:
    Jun 25, 2019
    Posts:
    8
    Hey so I setup the wantsToQuit and quitting listeners, what I want to create is a "Are you sure you wish to close" popup before closing the application below is my implementation but 9 times out of 10 my game would crash or the popup will appear and then it'd crash.

    So far its only popped up and functioned proprly before.

    It'd be much appreciated if somebody could give me some advice for approuching this issue.

    Code (CSharp):
    1.  
    2. public class WindowController : MonoBehaviour
    3. {
    4. public static WindowController instance;
    5. public UnityEvent ExecuteBeforeClose;
    6. bool quitConfirmation = false;
    7. public GameObject windowPopup;
    8.  
    9. [RuntimeInitializeOnLoadMethod]
    10. static void RunOnStart()
    11.     {
    12.         Application.wantsToQuit += WantsToQuit;
    13.         Application.quitting += Quit;
    14.     }
    15.  
    16. static bool WantsToQuit()
    17.     {
    18.         if (instance.quitConfirmation)
    19.         {
    20.             return true;
    21.         }
    22.         else
    23.         {
    24.             RequestQuitConfirmation();
    25.         }
    26.         return false;
    27.     }
    28.  
    29.     static void RequestQuitConfirmation()
    30.     {
    31.         DisplayOptionPane("Are you sure you wish to quit?", response =>
    32.         {
    33.             if (response)
    34.             {
    35.                 instance.ExecuteBeforeClose?.Invoke();
    36.             }
    37.         });
    38.     }
    39.  
    40.     static void Quit()
    41.     {
    42.             instance.quitConfirmation = true;
    43.             Application.Quit();
    44.     }
    45.  
    46.     static void DisplayOptionPane(string msg, Action<bool> response)
    47.     {
    48.         WindowPopup popup = instance.windowPopup.GetComponent<WindowPopup>();
    49.         instance.windowPopup.SetActive(true);
    50.         popup.header.text = "Notice";
    51.         popup.message.text = msg;
    52.         popup.accept.gameObject.SetActive(true);
    53.         popup.reject.gameObject.SetActive(true);
    54.  
    55.         popup.accept.onClick.RemoveAllListeners();
    56.         popup.accept.onClick.AddListener(() =>
    57.         {
    58.             response.Invoke(true);
    59.             instance.windowPopup.SetActive(false);
    60.         });
    61.  
    62.         popup.reject.onClick.RemoveAllListeners();
    63.         popup.reject.onClick.AddListener(() =>
    64.         {
    65.             response.Invoke(false);
    66.             instance.windowPopup.SetActive(false);
    67.         });
    68. }
    69.     }
     
    Last edited: Apr 12, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,711
    I would not use this method. For one, it behaves differently on different OSes, as documented in its API page.

    I just make an extra "Verify quit game?" scene, accept input in there, and either just quit the app, or else return to the main menu.
     
    SparrowGS likes this.
  3. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Just like Kurt said, the way I go about it is the "Exit/Quit" UI button pops another UI that asked "Are you sure?" and that one calls Application.quit
     
    Joe-Censored likes this.
  4. Lyraedan

    Lyraedan

    Joined:
    Jun 25, 2019
    Posts:
    8
    What if I wanted this popup to appear if the user pressed Alt+F4, or clicked the red close button? Regardless of what OS they are on?
     
  5. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Aren't alt F4 and the top close button OS dependent?
    I only do windows so I'm now sure how it works on various linux distros or mac

    I think you're starting to go into sketchy territory with the OS when doing stuff like that
     
    Joe-Censored and Kurt-Dekker like this.
  6. Zorkind

    Zorkind

    Joined:
    May 4, 2015
    Posts:
    55
    I think you are calling Quit a lot of times and causing a Stack Overflow.

    Read your output.log file in the AppData folder for your game, or put a break point in the editor, debug it out.

    I use wantsToQuit and it works flawlessly with Alt + F4 and X button events.

    It makes no sense for this event to be OS dependent, it is kind of obvious that Unity fires up this event on the appropriate way for each OS, depending on your build settings.

    The documentation has only one situation where it behaves differently that expected:
    IMPORTANT: The return has no effect on iPhone. Application can not prevent termination under iPhone OS.

    But then again, Apple is Apple, what to expect :-\
     
    Joe-Censored and Kurt-Dekker like this.
  7. Vivien_Lynn

    Vivien_Lynn

    Joined:
    May 17, 2019
    Posts:
    19
    This is how I currently do it:
    I hook into Application.wantsToQuit and link it to my CallQuitWindow-Function, which will open my confirmation window and return false, to abort the quitting.
    If the confirmation button is pressed, I unsubscribe my Function from the event, and than call Application.Quit() normally.
    I hope this guides you into the right direction.


    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class ConfirmQuitting : MonoBehaviour
    4. {
    5.     [SerializeField] private GameObject confirmationWindow;
    6.     private static ConfirmQuitting instance;
    7.  
    8.  
    9.     [RuntimeInitializeOnLoadMethod]
    10.     static void RunOnStart()
    11.     {
    12.         Application.wantsToQuit += CallQuitWindow;
    13.     }
    14.  
    15.  
    16.     private void Awake()
    17.     {
    18.         instance = this;
    19.     }
    20.  
    21.  
    22.     public static bool CallQuitWindow()
    23.     {
    24.         // Call Confirmation-Window and abort the quitting
    25.         instance.confirmationWindow.SetActive(true);
    26.         return false;
    27.     }
    28.  
    29.  
    30.     // Attached to Conformation-Button
    31.     public void Confirm()
    32.     {
    33.         // When pressing Confirmation-Button, unsubscribe from Event and the Quit Application
    34.         Application.wantsToQuit -= CallQuitWindow;
    35.         Application.Quit();
    36.     }
    37. }