Why is my quit button not working?

I’m new to Unity and I’ve been trying to make a quit button for my game. I attached a script called “Quit” to a button. In the “On Click” section, I put my button object in and set the function to “Quit.QuitGame”. For some reason, the button does nothing. It doesn’t even change colour when hovered on. Yes, interactable is on. The “Quit” script looks like this.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Quit : MonoBehaviour
{
    public void QuitGame()
    {
        Application.Quit();
    }
}

Does anyone know how to fix this?

1 Like
  1. What Platform you use? Application.Quit can behave differently on different platforms.
  2. Add Debug.Log("test"); before Application.Quit(); and check the console to discover whether your button works at all or not.

Application.Quit() does only work in build, not in Editor.

1 Like

Stopping the editor playmode is different.

#if UNITY_EDITOR
            EditorApplication.ExitPlaymode();
#else
            Application.Quit();
#endif
1 Like

Oh I did add a debug log to the script, but still nothing happens.

1 Like

Could you show a screenshot of your Button setup from the Inspector?

1 Like

I just noticed you said it doesn’t change color when hovering. If other buttons work, check that there isn’t something on top of the one not working. In builds the aspect ratio might be different, so the on top issue doesn’t happen. To test you can play with the editor Game tab ratio here:

If no buttons work, you might be missing an EventSystem in the scene, usually one is generated when you create any canvas in-scene. I think this is less likely though since you said it worked in build.

What is an EventSystem again?

Its a component called EventSystem, usually on a generated gameobject in the hierarchy called EventSystem. It has something to do with receiving events for a Canvas like mouse hover and button click.

You can easily add one if you go to a scene, right click → UI → EventSystem. As I said though it’s usually generated when you add a canvas to the scene, and wouldn’t cause an issue like this if the button works in the build.

1 Like

It worked! Thanks!

2 Likes