UI Button OnClick Only Works the First Time
Hello, I'm working in a game that has levels. Since each level is simply a unique wave of enemies, I have all the levels take place in the same scene. I control which level should be happening using a bool that becomes true when I click the button on the menu for that specific level. When I go from my level select menu (a separate scene) to my scene where the waves of enemies should spawn the first time, the bool becomes true and everything works, but the second time when I click the same button it still switches scenes, just the bool doesn't become true, so no enemies spawn or anything.
I've been struggling with this for about 2 hours now and I really have no idea why it's happening. The really confusing part for me is why the button calls both the function to make the bool true and the function that switches scenes the first time, but only the function that switches scenes the second time. If you need code I can give some, but pretty much how it's set up is I have a singleton between the two scenes that has both function to make the level bool true and the other function in the same script to make the bool false when all enemies are defeated.
Answer by lvskiprof · Aug 12, 2020 at 04:23 PM
What is the trigger that causes it to call the function that sets the bool to true? It sounds like when you switch scenes you need to reset that trigger back to its original state and then it will call your function.
Hard to be sure without seeing the code, but that seems to fit what you described.
Also, I just now realized that none of the buttons work the second time around if that helps anyone with solving this problem.
Ah, I've found the problem. Turns out many others have had the same problem. The buttons are missing the reference to my singleton because the link is broken when I enter the scene and come back. It looks like I have to create a script that will stay in the scene and have my Wave$$anonymous$$anager script reference that.
Answer by MidievalMan · Aug 13, 2020 at 06:14 PM
The trigger is the built in "On Click ()" thing that shows in the inspector when you click on a button in the editor. I dragged in my persistent singleton manager script to that and chose this function from it:
public void L3T()
{
L3 = true;
ResetTimer();
}
The reset timer function just sets a timer i use to know when to spawn enemies back to 0. Also, at the end of the level I run this function which is also in my persistent singleton manager script:
public void EndLevel()
{
turretStats.SaveTurretStats();
GameManagerScript.Instance.hasControl = false;
Time.timeScale = 0;
GameManagerScript.Instance.winMenu.SetActive(true);
GameManagerScript.Instance.enemiesKilledText.text = "Enemies Killed: " + GameManagerScript.Instance.enemiesKilled.ToString();
L1 = false;
L2 = false;
L3 = false;
L4 = false;
L5 = false;
L6 = false;
L7 = false;
L8 = false;
L9 = false;
L10 = false;
}
What I see happening in the inspector is that L3 (the bool controlling whether it is level three) starts out as being false. Then, when I click the button to enter level 3, I see it become true in the inspector. Then, at the end of the level it becomes false as expected because I run the function above after all the enemies in that level are destroyed. Finally, it remains false when I click the button to enter level 3 again. Hope this code helps, I'll keep troubleshooting. By the way, I've tested this on some of my other levels just to make sure it's not just level 3 and the rest of them work the same way.
Your answer
Follow this Question
Related Questions
How can I generate and setup buttons via code 1 Answer
is it possible to have two on click events in one button 1 Answer
How do I change a state in an FSM with an UI button? 0 Answers
Trying to increment a variable in a Coroutine on pressing a UI button 0 Answers
button pressing the button next to it?! 2 Answers