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 memetic007 · Sep 23, 2015 at 08:47 PM · application.quit

Application.quit() produces ungraceful exit

I attached the following code to an invisible GameObject.

After Building and Running, pressing the "X" key causes the game to halt BUT the program is left in a "program not responding" state rather than closing gracefully. What else do i need to do to get a game exe to end gracefully?

platform: Windows 7 pro.... Unity 5.2.1 f1 64bit

  void Update () {
             if (Input.GetKey(KeyCode.X)) {
                 Debug.Log("X code Detected");
                 Application.Quit();
     
             }
 }

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 etopsirhc · Sep 23, 2015 at 11:12 PM 0
Share

honestly not sure, but id guess something is left running that doesn't want to quit. so i would load an empty scene that has nothing in it but a script to quit as soon as it loads. this way everything that was running before will have been unloaded already and shouldn't interfere with the exiting.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by memetic007 · Sep 24, 2015 at 04:25 PM

Ok, I figured it out:

My game launches a long running coroutine that sends messages to a process outside of Unity via NetMQ (the .Net version of zeroMQ which has a Unity compatible version).

Application.Quit() apparently DOESN'T stop the coroutine, or at least it doesn't do so gracefully. Stopping the coroutine prior to calling Application.Quit() appears to have fixed the problem.

As a note for possible future exploration StopCoroutine("MyCoroutine") didn't seem to successfully stop the coroutine, even though I had launched it with StartCoroutine("MyCouroutine"). Instead I had the coroutine monitoring a variable and terminating itself when the value of the variable changed. Not elegant, but it worked. I am curious why StopCoroutine didn't work.

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 Max_Bol · Nov 10, 2020 at 08:02 PM 0
Share

This is a bit old, but the problem with coroutines can easily be avoided by defining the coroutine as a accessible parameter with a reference. Using a string as a reference to start a coroutine is to be avoided if the coroutine is long or looping.

A proper way of managing a coroutine so that you can properly stop it without any risk of loosing the reference is the following way:

 public IEnumerator CoroutineInstance;
 
     public void StartCoroutineInstance()
     {
         if(CoroutineInstance != null) {
             StopCoroutine(CoroutineInstance);
             CoroutineInstance = null;
         }
         CoroutineInstance = CoroutineName();
         StartCoroutine(CoroutineInstance);
     }
 
     public void StopCoroutineInstance() {
         if (CoroutineInstance != null)
         {
             StopCoroutine(CoroutineInstance);
             CoroutineInstance = null;
         }
     }
 
     public IEnumerator CoroutineName()
     {
         while (/*Whatever condition*/)
         {
             //Do whatever you need it to do.
             yield return null;
         }
     }

In the code above, you can change the IEnumerator content to whatever you need. StartCoroutine(String); will start a coroutine based on the string. If you call it twice, the 1st coroutine reference toward the string gets lost which explains why StopCoroutine(String) might not stop the coroutine afterward. Without referring the Coroutine in its own parameter, the only way to stop a coroutine who losts its reference string link is to use StopAllCoroutine();

In the example above, the function called StartCoroutineInstance(), which can be named as you want, can includes parameters and so, if needed, you can easily create a loop enumerator like this:

 public IEnumerator CoroutineInstance;
 
     public void StartCoroutineInstance(bool islooping)
     {
         if(CoroutineInstance != null) {
             StopCoroutine(CoroutineInstance);
             CoroutineInstance = null;
         }
         CoroutineInstance = CoroutineName(islooping);
         StartCoroutine(CoroutineInstance);
     }
 
     public void StopCoroutineInstance() {
         if (CoroutineInstance != null)
         {
             StopCoroutine(CoroutineInstance);
             CoroutineInstance = null;
         }
     }
 
     public IEnumerator CoroutineName(bool isLooping)
     {
         if (isLooping)
         {
             while (isLooping)
             {
                 //Do whatever you need it to do continuously.
                 yield return null;
             }
         }
         else
         {
             // Do whatever you need it to do only ONC$$anonymous$$
             yield return new WaitForSeconds(1f);
         }
 
     }

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

30 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

Related Questions

application.quit not quitting game? 2 Answers

Is there a simple way to display an error message? 1 Answer

Stand Alone App Does Not Quit Application 1 Answer

Unity2D: How to resume timer even after application has quit 1 Answer

BCE0034 Application.Quit 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