- Home /
How do I create a Exit/Quit Button
Hello I really need help. How do I create a exit/Quit button for my game? I want to attach it to a normal button, meaning that I have my own image but I just cant seem to find anything!
Thanks
Oh and drag the canvas to the on click click thing, but first attach the Script to the canvas and click QuitScript. Then choose the quit function and there you go!
Answer by zach-r-d · Jun 25, 2015 at 08:34 PM
If the button already exists, you're almost there! All that's left is to create a method in a script that looks like this:
void doExitGame() {
Application.Quit();
}
and then add it to the "On Click ()" section of the Button object.
Thank you for answering! under the function/button menu which one do I need to click?
Thanks for asking again!
Click the plus at the bottom right of the On Click () section, drag the script into the little box that says "None", then select doExitGame from the dropdown on the right.
The doExitGame does not appear when I click on the fuction menu!
Answer by BloodBTF · Jun 26, 2015 at 03:43 AM
You can do this: first go to gameobject: UI, then create a Canvas.then go to ui again and create button. Position it to your liking, Then create a c# script and call it "Quit script" or something, and in it, under the update void, enter this.enter code here
void QuitGame () {
Application.Quit ();
Debug.Log("Game is exiting");
//Just to make sure its working
}
Answer by Shadowlash1221 · Jun 25, 2015 at 08:27 PM
I think this will help
//Javascript
function OnGUI () {
// another code above...
var quitGame: boolean = GUI.Button(Rect(Screen.width/2 - 100, Screen.height - 200, 200, 20), "Quit Game");
if(quitGame) {
Application.Quit(); // As far as I know, this only works in the compiled game (.exe)
}
}
//C#
void OnGUI () {
// another code above...
bool quitGame = GUI.Button(new Rect(Screen.width/2 - 100, Screen.height - 200, 200, 20), "Quit Game");
if(quitGame) {
Application.Quit(); // As far as I know, this only works in the compiled game (.exe)
}
}
You should rather use the "new" UI system from Unity 4.6, old GUI is ... archaic ^^
Your answer
Follow this Question
Related Questions
2D Animation does not start 1 Answer
Dynamic Shader to Outline Adjacent Textures? 0 Answers
CharacterController or Collider/Rigidbody 2 Answers
Problem starting the game with a key 1 Answer