How to EXIT the APPLICATION or GAME in Unity through code

Introduction

In this arti­cle we see the instruc­tions we need exe­cute in order to quit the game in Uni­ty, then we will make a but­ton call the exit func­tion when pressed.

In the fol­low­ing video you can see the com­plete process in which we see how to close the game in Uni­ty, from the cre­ation of a sim­ple Can­vas to test the func­tion, to the script with the nec­es­sary functions.

🟢 How to EXIT the APPLICATION or GAME in Uni­ty through code
 

MOST SEARCHED VIDEOS FROM MY CHANNEL

ABOUT UNITY

ABOUT BLENDER

User Interface for Testing

We are going to use a very sim­ple user inter­face that will con­sist of a but­ton that when pressed will exe­cute the func­tion to close the game.

Script with the Function to Quit the Game

We will cre­ate a script called "Con­trol" that will be assigned to any GameOb­ject in the hier­ar­chy and will con­tain a pub­lic func­tion called "Exit" (see what is a method in pro­gram­ming and how to use it).

With­in the "Exit" method we will place a sin­gle instruc­tion that will be in charge of clos­ing the game.

Application.Quit();

The idea is that the but­ton on the user inter­face will exe­cute this func­tion when it is pressed. 

For this we select the but­ton in the hier­ar­chy and in the inspec­tor, in the OnClick sec­tion we click on the lit­tle plus sign to add a new field, we drag the GameOb­ject that has the pub­lic func­tion to close the game and using the drop-down menu we look for this func­tion in the list.

Conclusion

We've seen very sim­ply how to close the game at Uni­ty. For this we must exe­cute the "Application.Quit()" instruc­tion some­where in the code. In this case we exe­cute it when we press the exit but­ton of the user inter­face, but we could do it in dif­fer­ent ways.

Now it is time to think about what tasks we should car­ry out when clos­ing the game and make sure they are exe­cut­ed before the Quit instruc­tion. For exam­ple, what infor­ma­tion should we save to retrieve in the next session? 

EXTRA: Quit application with confirmation dialog

🎁 UNITY PACKAGE: Exit appli­ca­tion with con­fir­ma­tion dialog

ABOUT THIS SOLUTION

  • By click­ing in Down­load you will get a file with "Uni­ty Pack­age" exten­sion. Drag and drop that file into Uni­ty to import it.
  • When you import this Uni­ty Pack­age you will find a fold­er with a scene, a script and some extras.
  • Open the scene and you'll find detailed infor­ma­tion about how this solu­tions works.

🟢 How to quit appli­ca­tion with con­fir­ma­tion dia­log
 

Scroll to Top
Secured By miniOrange