- Home /
Application.Quit not working?!
Hello, Weel I have this script:
function Quit() {
print("Unity, please Quit!!!");
Application.Quit();
}
But it is not working. I call the functioni from a UI button. It does prints the string, but it doesnt quit!
What is wrong???
I do not see the year on any of these answers but the question was asked in 2015. It's 2016 now and I use 5.3.4, The Application.Quit function does not work . When you build and run from the internet or in system it does not make the game quit. Understood that in editor making the game quit would shut the editor down but when you build and run this function doe not really work as originally intended.
Please don't post an answer unless you're attempting to answer the question. Application.Quit works just fine and always has done to exit a standalone game... if it's not doing what you expected you need to show how you are calling it, and what you are trying to achieve.
UA is not a forum, the concept of 'necro-ing' is not applicable.
Answer by ElMenduko · Feb 11, 2015 at 05:50 PM
Your game doesn't close when testing it in the editor (pressing the play button) because it would close unity... and that means loosing everything, having to open it again, etc.
To test it you should build and run your game:
Fast way: Files>Build and Run
Slower way: Files>Build settings... (Some settings that may be useful for you)
When you build it you need to create a folder where unity will put the .exe and the other files in. Then you can run it as a standalone program. (Assuming you build and run for the Windows Standalone)
As stated in the Unity Scripting Reference : http://docs.unity3d.com/ScriptReference/Application.Quit.html
Quit is ignored in the editor or the web player.
Answer by TBruce · Mar 18, 2016 at 08:38 PM
You need to quit the game differently if it is being run in the editor. This does not close the Unity editor if you are running the game within Unity it only closes the game.
public void QuitGame()
{
// save any game data here
#if UNITY_EDITOR
// Application.Quit() does not work in the editor so
// UnityEditor.EditorApplication.isPlaying need to be set to false to end the game
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
This is such a simple thing, but it seems like the kind of thing that will save so much time over the months anD years of development. Thank you for sharing this gem! <3
Edit: Typo.
Answer by drod7425 · Feb 11, 2015 at 05:54 PM
If you are - in fact - trying to get the Unity editor to close from your UI, then here's the code to do it:
function Quit(){
EditorApplication.Exit(0);
}
CAUTION: This will close your unity application and you will lose any unsaved data.
BONUS EDIT: If you just want to stop the application in the editor, you can use the following. Note that it will not work in your builds:
function Quit(){
EditorApplication.isPlaying = false;
}
"EditorApplication.isPlaying = false;"
Exactly what I was searching, thanks :)
Answer by AngryBurritoCoder · Feb 11, 2015 at 05:43 PM
If you are in unity ( playmode in unity ), it does not quit, otherwise you would lose your work. Try run a built version of the game and it should work.
Answer by Unity_scat · Feb 28, 2016 at 09:18 AM
Apply
Application.Quit();
To your code, then go to file then build and run. The code will not work if you're testing it from the scene editor.
I've got a strange problem with this one... Application.Quit(); works for me when playing in editor but not working after build... Any idea?