Unity: How to Exit a Game on Button Click

Last updated on September 29, 2021 Pennywise Loading... One comment

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.

Subscribe
Notify of
guest
1 Comment
Inline Feedbacks
View all comments
floppa
floppa
6 months ago

cool

You May Also Like