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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Trying to quit

Discussion in 'Scripting' started by Zace666, Mar 26, 2017.

  1. Zace666

    Zace666

    Joined:
    Jan 28, 2014
    Posts:
    21
    Hi there - running unity 5.6
    I have a UI with 2 buttons, one to load the level and one to quit the game.
    I have added debug prints to ensure the code I have attached to the buttons gets called - it does.

    It doesnt load the level OR exit the game when I run from within unity.

    However if i build and run then the level is loaded or the game quits.

    WHY? I really would like to test within unity rather than compile and build every time I want to test something.

    Cheers
    C
     
  2. Ian094

    Ian094

    Joined:
    Jun 20, 2013
    Posts:
    1,548
    Check that the scenes are added to your build settings.

    Application.Quit() is ignored within the editor. It wouldn't make much sense if it wasn't.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    This. And not just the editor, there are a couple other platforms that have their own way of quitting, and ignore Application.Quit().
     
  4. Zace666

    Zace666

    Joined:
    Jan 28, 2014
    Posts:
    21
    So if it is ignored when I press the play button how do i test it? I was hoping it would just be the same as pressing the play button again to stop running my code.
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Throw in a Debug.Log right next to it. Then you'll see a message in the console each time it's called.
     
  6. WarmedxMints

    WarmedxMints

    Joined:
    Feb 6, 2017
    Posts:
    1,035
    Just do this in your quit function;

    Code (CSharp):
    1.     public void Quit()
    2.     {
    3.         #if UNITY_EDITOR
    4.             UnityEditor.EditorApplication.isPlaying = false;
    5.         #else
    6.             Application.Quit();
    7.         #endif
    8.     }
     
    Ian094, Kiwasi and Baste like this.