How to make a quit button work?

Hi! I am brand new to Unity, or coding at all (other than HTML). I am making a menu for the roll a ball tutorial, and everything works except the quit button. I am following Recorded Video Sessions on UI - Unity Learn to create the menu, but changing the bottom button to quit the game. I am using this code that does not work (C#):

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

public class Quitscript : MonoBehaviour
{

    void QuitGame()
    {
        Application.Quit();
        Debug.Log("Game is exiting");
    }
}
1 Like

BEFORE HATING ON ME! I have read other forums and none work.

Application.Quit() doesn’t do anything when playing in the Editor. Have you done a build and tried it?

yup. Just did it and nothing happend.

in the “On Click ()” window i have put the Quitscript in the bottom, but i read that i don’t need to add anything in the function part. Is this correct?

I’m not sure what you mean. You have to add QuitScript.QuitGame to the list of button click events in the Inspector or do it via code at run time.

1 Like

How do i do this?

https://docs.unity3d.com/Manual/script-Button.html
The list is at the bottom of the Inspector for the button

Yes, but when i press “No Funtcion” i can find the Quitscript button, but no quit option in there

Make QuitGame public

public void QuitGame()

Thanks! But when i continued wathcing the guide he added a quit button that also works when in the editor. Here is the script:

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

public class QuitOnClick : MonoBehaviour {

    public void Quit ()
    {
#if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
#else
        Application.Quit ();
#endif
    }

}
2 Likes

its already work bro, u need to build and run it in ur PC!

Yes, I’m sure they’ll value this input two and a half years later…bro…

2 Likes

I can’t get anything to work!!!

it is no work properly in game

If anyone’s still wondering what the solution is, there’s nothing wrong with the code, that works fine. But you need to make an empty game object and put the quit script onto that by itself.

And then in the OnClick part of the button, put the game object that has the script attached to it.

After that, you should be able to click the “no function” part and find what you’re looking for.
(The name of your script, and then the name of the void function. Example: QuitScript → QuitGame() then it should work.)

I think your problem was that you were putting the script straight from your ProjectFolders in the OnClick, instead of a game object.

(This also works for restart scripts, and I think scripts in general when you’re trying to attach them to the OnClick).

7 Likes

I don’t know why other people have struggled to get your code to work, I tried it and it worked first time perfectly. It’s a really simple solution so I guess the people complaining must have done something wrong or missed something out.

Personally I say thank you as this really helped me out and finished off my game nicely!

I’ve just said that I think your solution works perfectly, it did for me anyway so I have no idea why some people have posted negative comments. I wanted to say thank you for helping as this finished my game nicely.

This is how I did it:

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

public class QuitGame : MonoBehaviour
{

    public void QuitTheGame() {
        Application.Quit();
    }

}

I couldn’t understand why some people weren’t able to get this working as it worked perfectly for me. The only difference was that I added the ‘public’ keyword before ‘void’ when writing my function. Like you I assigned the script to an object which in my case was the button I used to trigger the Application.Quit(); function call.