Quit Game button will not work
I am taking an online course in Unity. The game we are making uses premade scripts. I can't get my quit button to work to end the game on either level. I have a GameOver canvas with a quit button, and the text on the button points to the quit game script, and the canvas is set to interactable, and points to the end game text and the correct script. No one else seems to be having this problem, and no one has answered my help request in the class message boards, so I am turning here for help.
Edited to add: it doesn't work in either the preview or after I build it.
I don't know how to do code - we learn how to use it next lesson. Basically, we were told to select a prewritten code from the provided scripts folder. I did see some answers to similar problems, but I haven't yet learned what to do with the provided pieces of code.
It wouldn't let me upload another picture to show the code, so I cut and pasted it (but I don't think that's the problem):
using UnityEngine; using System.Collections;
public class UIButtonQuitGame : MonoBehaviour {
public void quitGame()
{
//Closes the game
Application.Quit();
}
}
I edited the above post to reflect that the quit button doesn't work in the build, either. I had originally forgotten to mention that.
I am having the same issues. I actually have code working to quit the game from a menu, but once into my main game I cannot seem to get application.quit(); to work.
Sometimes I can get it to work by clicking on it repeatedly, but not all the time.
I have even tried putting a bool variable on a click event, and then having an if statement in Update that if the bool is true, then it quits the game. This doesn't work either. :(
Answer by yafo_unity · Aug 30, 2016 at 09:04 AM
void QuitGame() { #if UNITY_EDITOR if (Application.isEditor) UnityEditor.EditorApplication.isPlaying = false; #endif Application.Quit(); print("退出游戏"); }
@yafo_unity, thanks for the code, this helps quite a bit!
Answer by Jessespike · Oct 17, 2015 at 05:12 PM
Application.Quit() won't work in the Editor. It will work in builds though. If you want to make sure that the function is indeed getting called in the Editor, you can add a Debug.Log to notify yourself.
public void quitGame()
{
//Closes the game
Debug.Log("game quitted"); // Confirmation that the function is being called.
Application.Quit(); // This only works in builds, not in the Editor play modes.
}
Thanks, @Jessespike, but it's not working in the builds. :-(
The button goes dark for a second to indicate that I clicked it, but nothing else happens.
I have no idea what to do with the edit to the code (we learn that next week).
What you showed should work. There must be something else wrong then, but I'm unable to guess what it is.