Kinda Code
Home/C Sharp/Unity: How to Exit a Game on Button Click

Unity: How to Exit a Game on Button Click

Last updated: September 29, 2021

In Unity, you can programmatically quit a game in Unity by calling the Application.Quit() method. The code snippet below demonstrates how to exit a game when a button gets clicked:

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

public class ExitGame : MonoBehaviour
{
  Button myButton;

  void Start()
  {
    myButton = GetComponent<Button>();
    myButton.onClick.AddListener(ExitFunction);
  }

  void ExitFunction()
  {
    Application.Quit();
  }
}

Note that your game will NOT close when testing it in the Unity editor. To know if your code works correctly, run the game by selecting Files > Build and Run.

You should NOT implement the code above if you are making a game for iOS devices because it may violate App Store’s policy.