Application.Quit does not quit built Application
Hello everyone! This is my first time asking a question here, since this time Google could not help me :D
So I have my game and a button that calls following function:
public void ExitGame() {
Debug.Log("Function called.");
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
I'm sure I'm just missing some ridiculous detail, but I can't get the application to quit as soon as it's built. It definitely goes into the function since it logs the "Function called.", and in the editor, it exits the game as well. Just in the standalone app, nothing happens. Are there any settings I might have missed?
Thank you all in advance! :)
EDIT: I have a GameObject named "GameManagers" that has a QuitGame script on it. the script looks as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class QuitGame : MonoBehaviour {
private bool exitButtonPressed;
void Start() {
exitButtonPressed = false;
}
void Update() {
if(exitButtonPressed) {
ExitGame();
}
}
public void SetExitButtonPressed(bool wasPressed) {
exitButtonPressed = wasPressed;
}
private void ExitGame() {
#if UNITY_EDITOR
UnityEditor.EditorApplication.isPlaying = false;
#else
Application.Quit();
#endif
}
}
The method SetExitButtonPressed is called through clicking a button which uses the usual Button Event System Unity provides. It makes no difference if the button calls the ExitGame method directly, or if I set the Boolean exitButtonPressed to true, and call the method in the Update function as soon as the Boolean turns true. Both ways lead to everything workung fine in the editor, but in the built game, the function is not called.
Which platform do you target? There is no reason this would not work.
I built the game for windows - and I am at my wits' end with why it does not work, I know it should do...
How ExitGame
is called? Who calls it? When? How do you know the function is called in the built game?
Hmm, I just added a line of code that would just set a random GameObject in the Scene inactive, so that I would see whether the function was called or not, and the function is indeed not called in the built game. I've attached a screenshot of how the function is called.
EDIT: On the screenshot, it calls a function that sets a boolean to true, and the ExitGame function is called as soon as the boolean goes true. This was an attempt of a workaround that sadly didn't change the situation.
Can you edit your question and provide the relevant code? (the SetExitButtonPressed
and the function that checks whether the boolean is set to true)
Answer by MomosArts · Aug 15, 2018 at 04:16 PM
Okay, I eventually found a solution, although I have no idea why it doesn't work the other ways I've tried. Instead of having the QuitGame script on a separate GameObject, and then assigning the button the function by dragging the GameObject with the script on it into the Button Event in the inspector, I put the QuitGame script onto the button itself. And now it works without me having changed the code... I probably had a mess of protection levels there :)