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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Game doesn't quit on Application.Quit

Discussion in 'Scripting' started by Northernlightifly, May 12, 2018.

  1. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Hello!

    I have a timer countdown in my game and when the timer count down to zero I want my game to quit so I tried with the Application.Quit code but when I run the game it keeps counting to -1,-2 etc...does any of you know why?

    Here's what my code looks like

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using System.Threading;
    6.  
    7. public class CountDown : MonoBehaviour {
    8.  
    9.     public int timeLeft = 60;
    10.     public Text countdown;
    11.  
    12.  
    13.     void Start () {
    14.         StartCoroutine("LoseTime");
    15.         Time.timeScale = 1;      
    16.     }
    17.    
    18.     void Update () {
    19.         countdown.text = ("" + timeLeft);
    20.     }
    21.  
    22.  
    23.     IEnumerator LoseTime()
    24.     {
    25.         while (true) {
    26.             yield return new WaitForSeconds (1);
    27.             timeLeft--;
    28.         }
    29.     }
    30.  
    31.     public void QuitGame()
    32.     {
    33.         Application.Quit();
    34.     }
    35. }
     
  2. dgoyette

    dgoyette

    Joined:
    Jul 1, 2016
    Posts:
    4,113
    Application.Quit only works in a built version of the game, not in the editor. Try this, to ensure your Quit works wherever:

    Code (CSharp):
    1.                 Application.Quit();
    2.  
    3. #if UNITY_EDITOR
    4.                 UnityEditor.EditorApplication.isPlaying = false;
    5. #endif
     
  3. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
    You havent called the quit method...
    So it wont work.
     
  4. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Thank you! But I get a bunch of errors in the console column and am not able to run the game 8s
     
  5. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Okay, what do I have to do to call the quit method?
     
  6. NicBischoff

    NicBischoff

    Joined:
    Mar 19, 2014
    Posts:
    204
    When do you execute ‘QuitGame()’. It’s not in your code above.
     
  7. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    I see something like this?

    Code (CSharp):
    1. void OnApplicationQuit()
    2.     {
    3.         Debug.Log("Application ending after " + Time.time + "0");
    4.     }
    5. }
     
  8. Mauri

    Mauri

    Joined:
    Dec 9, 2010
    Posts:
    2,657
    You have this bit of code in your code:
    Code (CSharp):
    1. public void QuitGame()
    2.     {
    3.         Application.Quit();
    4.     }
    But you never execute "QuitGame()"... If time's up = call QuitGame().
     
  9. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Oh right! So like this?

    Code (CSharp):
    1.  public void QuitGame()
    2.     {
    3.         Debug.Log("Application ending after " + Time.time + "0");
    4.         QuitGame();
    5.     }
    6. }
     
  10. realm01

    realm01

    Joined:
    Sep 23, 2016
    Posts:
    2
    Call Application.Quit() after time runs out in LoseTime like this:

    Code (CSharp):
    1. IEnumerator LoseTime()
    2. {
    3.     while (true) {
    4.         yield return new WaitForSeconds (1);
    5.         timeLeft--;
    6.          if(timeLeft == 0) Application.Quit();
    7.     }
    8. }
     
  11. Northernlightifly

    Northernlightifly

    Joined:
    Feb 26, 2018
    Posts:
    71
    Got it thank you so much! :D