1
\$\begingroup\$

In the update, I have set up a counter so that after 140 frames, Unity will quit the game. But this quit command doesn't quit and it just continues to play the game. What am I doing wrong? I am using and testing in Unity 2D. There are no errors and all the prints are visible.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Normal_Car : MonoBehaviour
{
    private int counter = 0;

    public Vector3 finalPos = new Vector3(0.00f,0.00f,0.00f);

    public Transform t;

    // Start is called before the first frame update
    void Start()
    {
        t.GetComponent<Transform>();
    }

    // Update is called once per frame
    void Update()
    {
        print("Frame: " + counter.ToString());
        counter += 1;
        t.position += finalPos;
        if(counter >= 140)
        {
            Application.Quit();
            print("Quit!");
        }
    }
}
\$\endgroup\$
3
  • 2
    \$\begingroup\$ What do you mean by "doesn't work"? Your build crashes? Shows errors? It does something else? It does nothing? Is the print output visible? Are you testing this inside the editor or in a build? \$\endgroup\$
    – user35344
    Oct 24, 2020 at 16:44
  • \$\begingroup\$ Your edit only adresses part of my questions. Where are you testing this? Are the print outputs visible? Are there any errors? \$\endgroup\$
    – user35344
    Oct 24, 2020 at 17:08
  • \$\begingroup\$ I am testing in Unity and there are no errors. All the prints are visible. \$\endgroup\$ Oct 24, 2020 at 17:19

3 Answers 3

1
\$\begingroup\$

If you check out the documentation for Application.Quit it starts by saying this:

Quits the player application. Shut down the running application. The Application.Quit call is ignored in the Editor.

Application.Quit does nothing if you test inside the editor.

\$\endgroup\$
3
  • \$\begingroup\$ Okay, then is there any other way to text this without having to export something every time? Thank you for helping. \$\endgroup\$ Oct 24, 2020 at 18:04
  • \$\begingroup\$ Test what? Test the Application.Quit() implementation? Is it not enough to for an example debug log something before calling Application.Quit() and verifying that it is output correctly? Also a quick googling of "unity stop editor" yields this among other things: answers.unity.com/questions/161858/… if you want to stop the editor playing instead. \$\endgroup\$
    – user35344
    Oct 24, 2020 at 18:20
  • \$\begingroup\$ Yes, I meant to stop the editor as a place holder for testing my game. \$\endgroup\$ Oct 24, 2020 at 20:25
2
\$\begingroup\$

There were some comments here but no actual solution. The answers of course exist by searching but not in this post.

The ability to exit the game in BOTH standalone and editor mode is important for testing menus and other factors. Here is an example:

public void ExitGame()
{
    SaveGame();

    #if UNITY_STANDALONE
        Application.Quit();
    #endif

    #if UNITY_EDITOR
        UnityEditor.EditorApplication.isPlaying = false;
    #endif
}

private void SaveGame()
{
    Debug.Log("Saving Game...");
}

Hopefully this clarifies things a bit

\$\endgroup\$
0
\$\begingroup\$

I think it's this.Quit(); That might just be in WinForms though :)

\$\endgroup\$
8
  • \$\begingroup\$ "I think" — did you try researching or testing this suggestion to have more confidence that it applies in Unity? \$\endgroup\$
    – DMGregory
    Oct 25, 2020 at 12:22
  • \$\begingroup\$ @DMGregory I was following a tutorial for WinForms and he said 'do this.quit(); to make a quit button' \$\endgroup\$
    – privatedev
    Oct 25, 2020 at 12:34
  • \$\begingroup\$ Is this question about WinForms? \$\endgroup\$
    – DMGregory
    Oct 25, 2020 at 12:34
  • \$\begingroup\$ No, but WinForms uses C# and unity uses that too. \$\endgroup\$
    – privatedev
    Oct 25, 2020 at 12:39
  • \$\begingroup\$ this refers the the current object, the object of the class that owns the code where this is written. this will mean a different object depending on where it is written. You might want to read up a bit more on the topic. \$\endgroup\$
    – Vaillancourt
    Oct 25, 2020 at 13:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .