- Home /
Application.Quit() wont work in my coroutine.
I'm trying to close my application after I play a sound within a coroutine. The problem is it doesn't close when Application.Quit() is in the coroutine. (I have built and ran the game to test it)
if (Input.GetKeyDown(KeyCode.Z)
{
playerselect = "quit";
heart2.SetActive(false);
heartbroken.SetActive(true);
StartCoroutine(QuitGame());
}
IEnumerator QuitGame()
{
heartbroken.GetComponent<AudioSource>().Play();
yield return new WaitForSeconds(1);
Application.Quit();
}
Answer by NorthStar79 · Nov 21, 2017 at 07:39 AM
try this instead of Coroutine ;
if (Input.GetKeyDown(KeyCode.Z)
{
playerselect = "quit";
heart2.SetActive(false);
heartbroken.SetActive(true);
heartbroken.GetComponent<AudioSource>().Play();
Invoke("QuitGame",1f);
}
public void QuitGame()
{
Application.Quit();
}
well, I can not see any problem in his code in fact, so it should have work too. but if it is not, probably he is ter$$anonymous$$ating immediate all coroutines from elsewhere. I know its a long shot guess. but i've seen enough this kind of problems before. that's why my code worked.
Hello there,
Just FYI, an Invoke() can be used to call a function with a delay. You just have to pass it the name of the function as a string (case sensitive), and a delay. In this case, it's "QuitGame" and "1f", or 1 second.
You could also use InvokeRepeating, which takes one more float X as a third argument, and keeps calling the function every X seconds.
Now to be honest, the Coroutine code above doesn't have any errors. Either the coroutine gets stopped from somewhere, or the TimeScale is set to 0.
Hope that helps! ~LegendBacon
Your answer
![](https://koobas.hobune.stream/wayback/20220612143106im_/https://answers.unity.com/themes/thub/images/avi.jpg)