Search Unity

Making a quit button

Discussion in 'Immediate Mode GUI (IMGUI)' started by HarlequinGresh, Apr 12, 2010.

  1. HarlequinGresh

    HarlequinGresh

    Joined:
    Apr 12, 2010
    Posts:
    3
    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
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    Hi, welcome to the forum!

    You can use GUI.Button to display the button and Application.Quit to make the game quit:-
    Code (csharp):
    1. function OnGUI() {
    2.     if (GUI.Button(quitButtonRect, "Quit")) {
    3.         Application.Quit();
    4.     }
    5. }
     
  3. HarlequinGresh

    HarlequinGresh

    Joined:
    Apr 12, 2010
    Posts:
    3
  4. WoodyPal

    WoodyPal

    Joined:
    Apr 22, 2015
    Posts:
    2
    I am an absolute beginner and I want to do the same thing but can not understand what you put above.
     
  5. ajc200x

    ajc200x

    Joined:
    Dec 15, 2015
    Posts:
    3
    that guys answer is for js not c#
     
  6. Orami

    Orami

    Joined:
    Mar 12, 2015
    Posts:
    20
    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>
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using UnityEngine.UI;
    5. public class MyClass : MonoBehaviour
    6. {
    7. void Start() {}
    8. void Update() {}
    9. public void Quit()
    10.   {
    11.   Application.Quit();
    12.   }
    13. }
    14.  
    15.  
    4) Select the Canvas in the hierarchy
    5) Drag and drop the C# script onto the Canvas in the inspector
    6) Select the button
    7) scroll all the way down in the inspector to where it says On Click ()
    8) Drag the canvas object from the hierarchy to the inspector where it says GameObject (just under On Click)
    9) In the drop down menu in the inspector area where you drug the canvas to select MyClass > Quit()
     
    Polybiuss, Jriggrz and EdgeKing810 like this.
  7. Jriggrz

    Jriggrz

    Joined:
    Dec 9, 2016
    Posts:
    1
    thanks Orami works great :)
     
  8. CamoClayton

    CamoClayton

    Joined:
    Nov 29, 2016
    Posts:
    5
    Yes, thank you Orami, very simple and very easy to follow.