Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by heyhujiao · Dec 21, 2015 at 03:29 PM · if-statementsquitdouble

[HOW] Quit the game only after pressing Back button TWICE

I am making an android game. As a norm I think pressing the back button twice should close the application and thats what I aim to do in my game. However, I am not sure of how to code so that the if condition for the first time back button is pressed won't be skipped and cause the game to quit straight away. This is what I have now:

 void Update()
     {
 
         if (Input.GetKey(KeyCode.Escape))
         {
             isPaused = true;
             pauseGame();
 
             if (Input.GetKey(KeyCode.Escape))
             {
                 Application.Quit();
             }
         }
     }

Comment
Add comment
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

2 Replies

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

Answer by NoseKills · Dec 21, 2015 at 11:33 PM

You have basically 2 mistakes in your code.

Input.GetKey() returns true every time Update is called and the key is pressed... Just check the Unity Manual if you have trouble with Unity's funtions. Since Update is called all the time in rapid succession, it's very probable that the user will have the key pressed during multiple Update() calls even if he tries to press it very quickly. Use Input.GetKeyDown() instead because that happens only once when the button is pressed and doesn't happen again until it's released and pressed again.

The second issue is that even with GetKeyDown() both your if's will happen at once because the condition in them is the same.

You already pause your game when the button is pressed. Just use that as the condition for the second press.

 private void Update() {
         if (Input.GetKey(KeyCode.Escape)) {
             if (!isPaused) { // if game is not yet paused, ESC will pause it
                 isPaused = true;
                 pauseGame();
             } else { // if game is paused and ESC is pressed, it's the second press. QUIT
                 Application.Quit();
             }
         }
     }



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 heyhujiao · Dec 22, 2015 at 11:25 AM 0
Share

Thank you, it worked.

avatar image
2

Answer by saadali211 · Sep 09, 2019 at 02:44 PM

Here is the code

     int click = 0;
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.Escape))
         {
             click++;
             StartCoroutine(ClickTime());
 
               if (click > 1)
               {
                   print("Exit Game!");
               }
         }
     }
     IEnumerator ClickTime()
     {
         yield return new WaitForSeconds(0.5f);
         click = 0;
     }
 
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 dbdenny · May 19, 2020 at 02:30 AM 0
Share

worked for me, thx guy!

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.

Follow this Question

Answers Answers and Comments

35 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

The bool activates and deactivates but the script in the if statement doesn´t work 1 Answer

Problem with IF-statement 2 Answers

How to turn turn off/on a collider with a boolean from another script 1 Answer

AudioSource in Update 1 Answer

Project Spark to C# - Started To, While, and No Longer 1 Answer


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