- Home /
Make a button quit the game, Unity 4.6 UI C#
Hello, i would like some help with some code that could make it so that when the use presses a button on the screen,the exit button on my main menu, the game exits. I know that the Application.Quit() will allow me to exit, but how to i make it so that when a unity ui button is pressed, the game exits. Thank you
Answer by Mmmpies · Feb 02, 2015 at 04:54 PM
Create your script with a public function e.g. ClickExit() and put you Application.Quit() in there, also anything you need to do before quiting like save player data.
Put that script on your button and the select the button. Look in the inspector for the OnClick and click +
Then drag your button onto the slot that appears. After that you should be able to select your ScriptName - > ClickExit from the drop down to the right.
I would recommend to set the editor into a Stop Play$$anonymous$$ode as well.
#if UNITY_EDITOR
// set the Play$$anonymous$$ode to stop
#else
Application.Quit();
#endif
Thumbs up for the good advice @Tourist, although it wouldn't catch an issue in the Application.Quit line so the only way to prove it works for the game is to do a build -> run. Still good advice for debugging the button actually is recording a click without doing a build.
Any idea how to show a "Quit now"/"$$anonymous$$eep playing" dialog so that it would work with a keyboard or a game controller?
Easy, you can use "Debug.LogWarning([any string]);".
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Quit button 1 Answer
End/Pause Game! 2 Answers
Multiple Cars not working 1 Answer
Menu button in game isn't working 0 Answers