Unity: How to Exit a Game on Button Click

Updated: September 29, 2021 By: Pennywise 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>();

  void ExitFunction()

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.

Notify of
1 Comment
Inline Feedbacks
View all comments
2 years ago


Related Articles