Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Quitting game by pressing esc/escape key

Discussion in 'Scripting' started by Deleted User, Sep 28, 2018.

  1. Deleted User

    Deleted User

    Guest

    There's some tutorials and posts covering this but i find them all confusing, because i don't know how to enable the script or where to put it.

    I'm really new to this kinda stuff, i'd really appreciate it if someone would help me i need to have this school project done by tomorrow. :c
     
  2. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    stick this on an empty object in the scene.

    the update method runs every update, checks to see if the user pressed down the escape key, if so it call Application.Quit which quits the game.

    it's kida suedo so you're gonna have to fix typos

    Code (CSharp):
    1. public class GameManager : MonoDevelop {
    2.  
    3. void Update(){
    4.  
    5. if(Input.GetKeyDown(escape))
    6. Application.Quit();
    7.  
    8. }
    9.  
    10. }
     
  3. Deleted User

    Deleted User

    Guest

    i always get "Can't add script", it says The script needs to derive from MonoBehaviour
     
  4. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,905
    SparrowsNest made a couple mistakes in his script. The error you get now is because he said MonoDevelop instead of MonoBehaviour.

    This script is in the official docs:

    https://docs.unity3d.com/ScriptReference/Application.Quit.html

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. // Quits the player when the user hits escape
    5.  
    6. public class ExampleClass : MonoBehaviour
    7. {
    8.     void Update()
    9.     {
    10.         if (Input.GetKey("escape"))
    11.         {
    12.             Application.Quit();
    13.         }
    14.     }
    15. }
    This can be attached to ANY object in the scene. Camera, light, ground object, your character, an Empty, whatever. You just need one. This is because every active object in the scene will have all of its
    Update()
    functions called.
     
    Last edited: Sep 28, 2018
    Bunny83, SuchANameS and Joe-Censored like this.
  5. Deleted User

    Deleted User

    Guest

    I'm still getting the error :d
     
  6. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    Make sure the class name and the file name match exactly.
     
  7. Deleted User

    Deleted User

    Guest

    I have no idea what you're saying, i think this might be the issue then. Whats class name?
     
    Dunkin999 likes this.
  8. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,905
    In the official script I posted, they called the class ExampleClass. This means it should be in a file called ExampleClass.cs and added to an object.

    Is your error message about GameManager or ExampleClass?
     
  9. Deleted User

    Deleted User

    Guest

    Matching names allowed me to add script, tested game after build and it works now. Thanks for hand all c: i didn't expect to get answers this quick.
     
  10. vladimirsmart

    vladimirsmart

    Joined:
    Nov 27, 2017
    Posts:
    25
    The script doesn't work for me. I get no errors, but nothing happens.
     
    Dunkin999 and BatFlaps like this.
  11. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Is it attached to a gameobject running in the scene?
     
  12. vladimirsmart

    vladimirsmart

    Joined:
    Nov 27, 2017
    Posts:
    25
    Yes, its attached to a empty gameobject in the scene.
     
  13. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Add Debug.Log statements everywhere to figure out what path the code is taking.
     
  14. deaa86_arch

    deaa86_arch

    Joined:
    Mar 1, 2022
    Posts:
    5
    This video shows how to quit a Unity game or app by clicking a button or pressing keys on the keyboard.
     
  15. SPELBADRUM

    SPELBADRUM

    Joined:
    Feb 16, 2021
    Posts:
    2
    i hope this helps your question here (i just realized the date was long time ago lol)

    Code (CSharp):
    1. if (Input.GeyKey(KeyCode.Escape)) //Get the Esc / Escape key via the keycode enum
    2.             Application.Quit(); //Quit the game lol
     
    Last edited: Aug 20, 2022
  16. definitelit

    definitelit

    Joined:
    Sep 15, 2022
    Posts:
    2
    um does any of this still work?
     
  17. Dunkin999

    Dunkin999

    Joined:
    Jan 14, 2023
    Posts:
    1
    Your code does not make my game exit
     
  18. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,965
    Please don't necro post. Make your own post, and remember: we cannot read your mind.

    When you make a new post, here is how to report your problem productively in the Unity3D forums:

    http://plbm.com/?p=220

    This is the bare minimum of information to report:

    - what you want
    - what you tried
    - what you expected to happen
    - what actually happened, especially any errors you see
    - links to documentation you used to cross-check your work (CRITICAL!!!)

    Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

    How to do tutorials properly, two (2) simple steps to success:

    Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That's how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

    Fortunately this is the easiest part to get right: Be a robot. Don't make any mistakes.
    BE PERFECT IN EVERYTHING YOU DO HERE!!


    If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

    Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

    Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

    Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there's an error, you will NEVER be the first guy to find it.

    Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!
     
  19. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    1,905
    It's true Unity's documentation needs to be updated, for multiple reasons. Application.Quit() does not work in the Editor anymore, and if you're using the new Input System it's kind of a hassle to poll for specific keys.

    Here's a more full-featured version of the script, which I use during development. Also, since the usual Pause shortcut disappears if you turn off Unity Shortcuts in Play Mode, I added it back with an F12 poll.

    Code (CSharp):
    1. // QuitAppOnEscape.cs
    2. //
    3. using UnityEngine;
    4. #if ENABLE_INPUT_SYSTEM
    5. using UnityEngine.InputSystem;
    6. #endif
    7.  
    8. namespace halley
    9. {
    10.     //
    11.     // Throw this on any always-present game object such as Main Camera
    12.     // in any generic super-simple apps for deployment on Android or
    13.     // Standalone.
    14.     //
    15.     // Unity has two input handling setups.
    16.     //  - the "legacy" Input handling setup
    17.     //  - the "new" InputSystem handling setup
    18.     //
    19.     // This class uses the new InputSystem if it has been enabled.
    20.     //
    21.     // Project Settings >
    22.     //    Player >
    23.     //       Other Settings >
    24.     //          Configuration >
    25.     //             Active Input Handling = Both
    26.     //
    27.     public class QuitAppOnEscape: MonoBehaviour
    28.     {
    29.         [Tooltip("If true, Quit works in built apps outside editor too.")]
    30.         public bool standalone = false;
    31.  
    32.         void Awake()
    33.         {
    34. #if UNITY_EDITOR
    35.             if (!standalone)
    36.                 if (UnityEditor.EditorApplication.isPlaying)
    37.                     Destroy(this);
    38. #endif
    39.         }
    40.  
    41.         void Update()
    42.         {
    43. #if ENABLE_INPUT_SYSTEM
    44.             if (Keyboard.current.escapeKey.wasReleasedThisFrame)
    45. #else
    46.             if (Input.GetKeyUp("escape"))
    47. #endif
    48.             {
    49.                 Debug.Log($"Quitting App on Escape Key struck.");
    50.                 Quit();
    51.             }
    52.  
    53. #if ENABLE_INPUT_SYSTEM
    54.             if (Keyboard.current.f12Key.wasReleasedThisFrame)
    55. #else
    56.             if (Input.GetKeyUp("f12"))
    57. #endif
    58.             {
    59.                 Debug.Log($"Pausing Game on F12 Key struck.");
    60.                 Pause();
    61.             }
    62.  
    63.         }
    64.  
    65.         void Pause()
    66.         {
    67. #if UNITY_EDITOR
    68.             UnityEditor.EditorApplication.isPaused = true;
    69. #endif
    70.         }
    71.  
    72.         void Quit()
    73.         {
    74. #if UNITY_EDITOR
    75.             // Application.Quit() does not work in the editor so
    76.             // UnityEditor.EditorApplication.isPlaying need to be
    77.             // set to false to end the game.
    78.             UnityEditor.EditorApplication.isPlaying = false;
    79. #else
    80.             Application.Quit();
    81. #endif
    82.         }
    83.     }
    84. }
    85.  
     
    Daniel-Talis likes this.