• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
7
Question by Drummermonkey01 · Jun 25, 2015 at 07:43 PM · 2d

How do I create a Exit/Quit Button

Hello I really need help. How do I create a exit/Quit button for my game? I want to attach it to a normal button, meaning that I have my own image but I just cant seem to find anything!

Thanks

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BloodBTF · Jun 26, 2015 at 03:44 AM 0
Share

Oh and drag the canvas to the on click click thing, but first attach the Script to the canvas and click QuitScript. Then choose the quit function and there you go!

3 Replies

· Add your reply
  • Sort: 
avatar image
16
Best Answer

Answer by zach-r-d · Jun 25, 2015 at 08:34 PM

If the button already exists, you're almost there! All that's left is to create a method in a script that looks like this:

 void doExitGame() {
     Application.Quit();
 }

and then add it to the "On Click ()" section of the Button object.

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Drummermonkey01 · Jun 26, 2015 at 01:52 PM 0
Share

Thank you for answering! under the function/button menu which one do I need to click?

Thanks for asking again!

avatar image zach-r-d · Jun 26, 2015 at 08:20 PM 0
Share

Click the plus at the bottom right of the On Click () section, drag the script into the little box that says "None", then select doExitGame from the dropdown on the right.

avatar image Drummermonkey01 · Jun 27, 2015 at 01:45 PM 0
Share

The doExitGame does not appear when I click on the fuction menu!

avatar image zach-r-d · Jun 27, 2015 at 01:47 PM 1
Share

Try putting "public" in front of "void doExitGame"

avatar image Drummermonkey01 · Jun 27, 2015 at 04:05 PM 1
Share

Thank you so much it works perfectly!

Show more comments
avatar image
1

Answer by BloodBTF · Jun 26, 2015 at 03:43 AM

You can do this: first go to gameobject: UI, then create a Canvas.then go to ui again and create button. Position it to your liking, Then create a c# script and call it "Quit script" or something, and in it, under the update void, enter this.enter code here

 void QuitGame () {
 Application.Quit ();
 Debug.Log("Game is exiting");
 //Just to make sure its working
 }



Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Shadowlash1221 · Jun 25, 2015 at 08:27 PM

I think this will help

  //Javascript
          
          function OnGUI () {
          
          // another code above...
          
          var quitGame: boolean = GUI.Button(Rect(Screen.width/2 - 100, Screen.height - 200, 200, 20), "Quit Game");
          
          if(quitGame) {
          
          Application.Quit(); // As far as I know, this only works in the compiled game (.exe)
          
          }
  }
  
  
   //C#
      
      void OnGUI () {
      
      // another code above...
      
      bool quitGame = GUI.Button(new Rect(Screen.width/2 - 100, Screen.height - 200, 200, 20), "Quit Game");
      
      if(quitGame) {
      
      Application.Quit(); // As far as I know, this only works in the compiled game (.exe)
      
      }
      
   }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image KdRWaylander · Jun 26, 2015 at 06:36 AM 0
Share

You should rather use the "new" UI system from Unity 4.6, old GUI is ... archaic ^^

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

2D Animation does not start 1 Answer

Accelerating speed 2 Answers

Object Squeezing Between Tiles 0 Answers

Teleport 2D Player to another room 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges