Making a quit button

I’m just trying to make a simple front end/HUD and can’t work out the command to make a button quit the game.
Can anyone help me out?

Thanks

Hi, welcome to the forum!

You can use GUI.Button to display the button and Application.Quit to make the game quit:-

function OnGUI() {
    if (GUI.Button(quitButtonRect, "Quit")) {
        Application.Quit();
    }
}

thanks!

I am an absolute beginner and I want to do the same thing but can not understand what you put above.

that guys answer is for js not c#

For Unity 5.x

You will need to:

  1. Create a Canvas in the Hierarchy (Right click > UI > Canvas)
  2. Make a button (Right click > UI > Button)
  3. Create a new C# script and name it MyClass

<MyClass.cs>

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class MyClass : MonoBehaviour
{
void Start() {}
void Update() {}
public void Quit()
  {
  Application.Quit();
  }
}
  1. Select the Canvas in the hierarchy
  2. Drag and drop the C# script onto the Canvas in the inspector
  3. Select the button
  4. scroll all the way down in the inspector to where it says On Click ()
  5. Drag the canvas object from the hierarchy to the inspector where it says GameObject (just under On Click)
  6. In the drop down menu in the inspector area where you drug the canvas to select MyClass > Quit()
3 Likes

thanks Orami works great :slight_smile:

Yes, thank you Orami, very simple and very easy to follow.