- Home /
Exit application
Hi, i need to exit from an application if i press a key
i wrote this:
if(Input.GetKey(KeyCode.Space))
{
if(pos == 0)
Application.LoadLevel("Scene");
else if(pos == 1)
Application.Quit();
}
but Application.Quit(); doesn't work, can someone help me ? thank you:)
Just to check. Are you running this from the editor or webplayer? Application.Quit will not work on either.
yes, i set pos to 1 or 0, based on the key that you pressed
That is your problem then. Try building a standalone($$anonymous$$ac/Win) version of your game and see if the application quits like you expect it to.
You can use UnityEditor.EditorApplication.isPlaying = false;
to stop the application from running in the editor.
Answer by rancid1 · Apr 04, 2014 at 03:17 PM
You can change your line 6 into the following snippet and it will behave far more intuitively for developer usage. (In that it will feed a message to the developer and explain why nothing is happening.) Stuff like this doesn't exactly make sense that it's not in the base engine... But there you have it.
if (Application.isEditor)
Debug.Log("Attempted to quit from the Editor.")
else if (Application.isWebPlayer)
Debug.Log("Attempted to quit from the Web Player.")
else
Application.Quit();
Answer by KittyKatKat · Apr 03, 2014 at 06:33 PM
Should be Input.GetKeyDown(KeyCode.Space)) or GetKeyUp (Depending on how you want it).
well... $$anonymous$$inda.
The OP's code should work, it might just try to load the level (or quit) a bunch of times. I'm not sure that just changing to keyup or keydown will actually solve the problem.
I just tested. I don't know, it seams to work for me. OP, is your class made to DontDestroyOnLoad(); ? Because if it is, it might be made into a loop of scene loading (but in the end, it should switch to the scene anyway) Another solution would be to put in the index of your scene, rather than the name of it, maybe?