- Home /
UI Button not calling function properly?
So I have a save point in my game where if you approach it and press a button, you enable a Canvas with three button options. I also set Timescale to zero and make it so the player can't move. This happens in Update() on a script attached to my save point object..
if (playerHere && player.GetComponent<PlayerMovementTest> ().control == true && Input.GetButtonDown ("Submit"))
{
eventSystem.GetComponent<EventSystem> ().SetSelectedGameObject(saveButton);
player.GetComponent<PlayerMovementTest> ().control = false;
Time.timeScale = 0;
saveMenu.SetActive (true);
}
So far so good. I have three buttons, Load, Save, and ClearSaves. Each button calls a function that looks something like this:
public void SaveButton ()
{
//save the game, and take note of this save point specifically
player.GetComponent<PlayerMovementTest> ().spawnPointNumber = spawnLocation;
player.GetComponent<PlayerMovementTest> ().Save ();
Debug.Log ("You saved at spawn point number " + spawnLocation);
//turn off the canvas, and resume play
saveMenu.SetActive (false);
Time.timeScale = 1;
player.GetComponent<PlayerMovementTest> ().control = true;
Debug.Log ("End of function");
}
Now the last half of this is what's giving me issue. When looking in the Console, I get the "End of function" log just fine, but the canvas never disables itself. and the timescale never gets set back either, despite me putting in a test Debug.Log that claimed it had returned to a value of 1, like it should. Everything just stays frozen and the buttons are still on screen.
But to make things even MORE confusing, just to test I tried calling the same SaveButton() function in Update through a keystroke.
if (Input.GetKeyDown(KeyCode.Escape))
{
SaveButton ();
}
And for some reason, this works! So apparently the button just isn't doing its job, despite the Debug.Log telling me otherwise. Does anyone know what on earth is happening here?
I'm using Unity 5.6 by the way.
Answer by LCStark · Sep 17, 2018 at 02:08 PM
Put a Debug.Log
in the first block of code (the one where you stop the time and show your menu). Maybe it immediately pauses again after you've unpaused it?
You're right, it does! The "Submit" Button is used for both opening the menu and pressing the button (which closes the menu), so it must be doing both in the same frame. That would explain why it works fine if I use the escape key ins$$anonymous$$d. Thank you!
Answer by Jan_Julius · Sep 17, 2018 at 08:30 AM
Have you tried running it without setting timescale to 0? does this give you the same behaviour?
I did, and it still left the canvas on screen, no difference.
What type is save$$anonymous$$enu?
Have you tried calling save$$anonymous$$enu.gameobject.setactive(false)
Capitalize where needed ^
save$$anonymous$$enu is a public GameObject that I assigned in the inspector. Just tried changing the code to that but it still gave the same effect... no luck :/ Thanks for the suggestion though.
Your answer
Follow this Question
Related Questions
cant run function when button pressed 1 Answer
Button Action Triggering Multiple Times Per Click? 1 Answer
Unable to show button after an event 0 Answers
Multiple Cars not working 1 Answer