- Home /
How to delay few seconds when I quit the application?
For some reason, I hope the application will delay 5 seconds when I quit the application. It does not means I want the application delay 5 seconds and start to quit. The things I want is delay 5 seconds when the application is quitting. But I have no idea how to do it. I use "yield return new WaitForSeconds(5);" in "OnApplicationQuit()". But it didn't happen.
Yield is an odd thing to get used to, here's my 2 cents.
The OnApplicationQuit method is called literally RIGHT before the application exits. With the way yield works, it's allowing the function to continue which allows the program to exit immediately.
Basically don't use yield in OnApplicationQuit.
As the Commander just asked, why do you need to pause inside of OnApplicationQuit? What are you trying to do within the 5 seconds that yield would give you?
I have to do things in "OnApplicationQuit" because I have some resource need to be released. If user doesn't release the resource and quit the application. I have to do it in "OnApplicationQuit" by myself. This is the reason.
Answer by morteza_asadpour · Dec 02, 2013 at 06:59 AM
public IEnumerator Run() { yield return new WaitForSeconds(5f); Application.Quit(); } void Start() { StartCoroutine(Run()); }
Answer by anthot4 · May 08, 2018 at 08:55 AM
I don't understand why you would want this, as if the user quits your game they want it to be an instant process. But anyway you can either use @morteza_asadpour way of a coroutine or an invoke function like:
Invoke("QuitGame",5.0f); // Put this in where the user triggers exiting the game, it calls the QuitGame function in 5 seconds.
void QuitGame(){ Application.Quit }