Trying to get the Exit Game to work.

Hello:

I’ve been going through the tutorials for Unity and enjoy it so far, one issue I came across when trying to modify one of the projects to have an option to exit the game. I created a UI Text and placed it where I wanted, the problem came with the script.

When the “Q” button is pressed I want the Unity Editor to stop if I’m there and exit the application if I’m running stand alone, here’s my script:

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

public class QuitGame : MonoBehaviour {

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            FinishGame();
        }
    }

    void FinishGame()
    {
        if (UnityEditor)
        {
            UnityEditor.EditorApplication.isPlaying = false;
        }
        else
        {
            Application.Quit();
        }
    }
}

When I compile it it give me the error “‘UnityEditor’ is a namespace but is used like a variable”. I’ve searched the help as well as Googled the hell out of it and I can’t find anything that solves the issue.

Relp? :slight_smile:

Thanks!

I managed to figure it out for those following the question, turns out I was using the wrong syntax, here’s the changes I made to get it working:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif

public class QuitGame : MonoBehaviour {

    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Q))
        {
            FinishGame();
        }
    }

    void FinishGame()
    {
        #if UNITY_EDITOR
        EditorApplication.isPlaying = false;
        #else
        Application.Quit();
        #endif
    }
}

Cheers!