Application.Quit() stops working after boing back to Start Scene.

This is hard to explain–but, here I go.
I run the game in the editor, everything runs as expected. Then, I do a build and test and things are going wonky. My most frustrating bug is the Exit Button in the games StartScene. It works fine the first time! But if I leave this scene, (say going to Level1) and come back. The Application.Quit() stops working. I am going to build a way to view output in the build–but, this little bug is a stinker and I am hoping to kill it before I do so.

Code example:

public void OnExitClick()
    {
        //Start screen.
        if (CurrentLevel == 0)
        {
            if (Time.timeSinceLevelLoad > 2)
            {
                Game.Message("Application Quit called.");
                Application.Quit();
           
            }//end if

        }//end if

        //Levels
        if (CurrentLevel > 0)
        {
            Game.Message("Loading Start Scene.");
            SceneManager.LoadScene(0);
       
        }//end if
       
    }//end function

Looks like you’re not updating your CurrentLevel value when you go back a level.

1 Like

Hmm. You’re probably right Zalosath. Let me check! This might be the problem for sure. Should have made some debugging first thing. Thanks so much. Will report back.

Looks like the simple solution fixed the problem. Thanks so much for getting my brain going Zalosath!

Here’s the code now:

public void OnExitClick()
    {
        //Current level.
        int level  = SceneManager.GetActiveScene().buildIndex;

        //Exit level.
        if (level == 0)
        {
            Game.Message("Application.Quit() called.");
            Application.Quit();

        }//end if

        //Start screne.
        else if (level > 0)
        {

            Game.Message("Loading StartScene.");
            SetCurrentLevel(0, _startState);

        }//end if
    }//end function
2 Likes