Boris Nacev
Boris Nacev

Reputation: 11

Button in unity doesn't work, how can i fix it?

I am new to unity and I am still learning. I am making a game and its going great but I have a problem with a Quit button.

It doesn't use the script attached to the On Click () section in the button menu.

There's an EventSystem in the Hierarchy, the Force Module Active is checked, the button is a child to a canvas. I don't know why the button isn't working

Here is the code in the script attached to On Click ()

public void Quit()

{
    Debug.Log ("YES IT WORKS");
    Application.Quit ();
}

I want the program to close when the button is pressed but when the button is pressed nothing happens!

Upvotes: 1

Views: 3042

Answers (3)

LiefLayer
LiefLayer

Reputation: 1073

Are you in the Editor? Application.Quit() only works when you build the application (for PC and Android. It should not work on iOS, and even if it works it should display as a crash so it's not good).

Upvotes: 0

Hamza Tahir
Hamza Tahir

Reputation: 92

Solution

Instead of using Application.Quit() try to shift the your working scene to the home screen .Your every game should start and end at the same level which is also known as the main scene of the the game. Secondly before shifting to other scene you should destroy your player and save the scores in the player pref .

Player Pref:

The library which helps the user to store the data and show them on the main screen , Hence the use of database will not be there in order to save the data for the small games . You are beginner so you should look at these all thing on You tube or the unity website documentation available on its website

Upvotes: 0

Jambla
Jambla

Reputation: 478

The function is not being called when the button is pressed which means that you have not selected the function in the onClick settings. To do this, ensure that the script is attached to the button and then dragged into the onClick box of the button in the inspector. Next, click on the dropdown and hover over the name of the attached script and select the function that you want to call.

Upvotes: 2

Related Questions