Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Shuting down the game

Discussion in 'Scripting' started by AGSUnity, May 17, 2015.

  1. AGSUnity

    AGSUnity

    Joined:
    Apr 5, 2015
    Posts:
    8
    Hello Unity community, it's me, AGSUnity.
    I want to ask you, scripting experts, which C# command I need to use to shut down the game.
    If someone helps me, I will be very pleased. :D
     
  2. proandrius

    proandrius

    Unity Technologies

    Joined:
    Dec 4, 2012
    Posts:
    544
    Code (csharp):
    1. Application.Quit();
     
    Last edited: May 17, 2015
    karl_jones likes this.
  3. Jamster

    Jamster

    Joined:
    Apr 28, 2012
    Posts:
    1,102
    Shouldn't that be
    Code (csharp):
    1. Application.Quit();
    with brackets?
     
    proandrius likes this.
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,943
    Outside of the editor and web player, you use Application.Quit(). Within the editor you need to change the property isPlaying on the EditorApplication class.

    The web player doesn't really have a way to stop aside from browsing to a new URL.

    Code (csharp):
    1. #if UNITY_EDITOR
    2. UnityEditor.EditorApplication.isPlaying = false;
    3. #elif UNITY_WEBPLAYER
    4. Application.OpenURL("some url such as your homepage");
    5. #else
    6. Application.Quit();
    7. #endif
    Additional platform defines can be found here.
    http://docs.unity3d.com/Manual/PlatformDependentCompilation.html
     
    Last edited: May 17, 2015
    calmcarrots likes this.
  5. AGSUnity

    AGSUnity

    Joined:
    Apr 5, 2015
    Posts:
    8
    But with abrackets don´t work, it keeps saying "Unexpected symbol '(' in class, struct or interface member declaration"
    If someone can solve this, thanks
     
  6. karl_jones

    karl_jones

    Unity Technologies

    Joined:
    May 5, 2015
    Posts:
    8,222
    Did you put Application.Quit inside of a function call? Can you post your code?
     
  7. AGSUnity

    AGSUnity

    Joined:
    Apr 5, 2015
    Posts:
    8
    Here:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GameShutdown : MonoBehaviour {
    5.    
    6.     Application.Quit();
    7.    
    8.     }
    9.  
     
  8. Disastercake

    Disastercake

    Joined:
    Apr 7, 2012
    Posts:
    317
    You need to put it inside of a method that is then called like...

    Code (CSharp):
    1. public class GameShutDown : MonoBehaviour
    2. {
    3.     public void Update()
    4.     {
    5.         if (Input.GetKeyDown (KeyCode.Escape))
    6.             ExitGame ();
    7.     }
    8.  
    9.     private void ExitGame()
    10.     {
    11.         Application.Quit();
    12.     }
    13. }
    The code above will exit the system when Escape key is pressed. If you want a button that will do it, you should follow the tutorials on uGUI here: https://unity3d.com/learn/tutorials/modules/beginner/ui/ui-button
     
    karl_jones likes this.
  9. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    Hmmm,
    I need more information on this.

    I have a method
    1. public void Update()
    2. {
    3. if (Input.GetKey ("escape"))
    4. Application.Quit();
    5. }
    and it works in my standalone game however in the mobile version it does not.

    How do I use script to end the game in my mobile version??
     
    Last edited: May 29, 2015
  10. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    I now have an Exit button that runs the following script:
    1. public class QuitMobileGameOnClick : Monobehaviour {
      1. public void QuitMobile()
      2. {
      3. Application.Quit();
      4. }
    2. }
    When I run on my mobile device and press Exit, the game quits and returns to the phone homepage with the following error.
    "Unfortunately,
    Game has stopped."

    How do I properly exit the game??
     
  11. Disastercake

    Disastercake

    Joined:
    Apr 7, 2012
    Posts:
    317
    I believe that on mobile devices you are not really suppose to have a "Terminate My App" button. The nature of mobile apps are that the operating system handles shutting down the apps itself. For example on the iPhone you double click the main circle button on the phone, then can swipe up to close the apps.

    So basically, when developing for mobile you shouldn't have a close app button. You should probably just have a script that removes this exit app button from the game if you are not on a standalone build. To do that, you'd add this to your script:
    Code (csharp):
    1.  
    2. void Start ()
    3.             {
    4.                 if (Application.isMobilePlatform)
    5.                     gameObject.SetActive(false);
    6.             }
    7.  
     
  12. TGKG

    TGKG

    Joined:
    Dec 31, 2014
    Posts:
    180
    Thank you DisasterCake for the idea.

    So, I have done some more testing and have found the following:
    With my code on the Exit Button (ie Application.Quit(); )
    In my Build Settings / Player Settings / Other Settings, when "Multithreaded Rendering" is checked On, then I get the error. When it is checked Off, I DO NOT get the error and the game exits to the phone home screen ??? Not entirely sure why this fixes the error ???

    When I add your code above
    1. {
    2. if (Application.isMobilePlatform)
    3. gameObject.SetActive(false);
    4. }
    to my exit button and press the phones "Back Button" it ends the game, however it does not exit the game back to the phone homescreen and the game just hangs there until I press the center phone button. Interesting!!
     
  13. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Or better yet use a pre-processor directive that strips out the code entirely.
     
  14. Disastercake

    Disastercake

    Joined:
    Apr 7, 2012
    Posts:
    317
    I am not sure why this is. Removing multithreaded rendering sounds like it would hurt performance of your game, so it'd be best left on probably. I personally would just scrap the idea of trying to exit the program in-game from mobile devices and leave it up to the operating system to handle shutting down the apps.

    This should actually disable the button if it is attached to the button, and it shouldn't really affect how it functions when clicked since it is ran on Start() method, a method ran once when the object is enabled.


    No problem. We all start somewhere! =)
     
  15. Disastercake

    Disastercake

    Joined:
    Apr 7, 2012
    Posts:
    317
    As I was reading through the changelogs in Unity 5.0.2 released today, I saw a line that reminded me of this thread:

    Hopefully that fixes the issue you were having if you haven't fixed it with the suggestions above already.