- Home /
Unity 4.6 Button onClick not working in Scenes
I have 3 scenes setup:
Splash
Menu
Story
There's a coroutine setup to wait 5 secs then load the Menu scene. The Menu scene has a canvas with a button along with the automatically added EventSystem. From my GameController script I can attach a onClick.AddListener to the button which loads the next scene (Story scene) when clicked.
But on the Story scene I can't get the onClick.AddListener to work, it seems to not be triggering when the button is clicked. The Story scene has the same setup as the Menu scene (Canvas, Button, EventSystem).
I'm not sure if it's a bug of Unity 4.6 or I'm just doing it wrong. Can I have different Canvas/Buttons in different scenes? Thanks
UPDATE -- **
I think I fixed it. I think I was referencing the wrong thing. I was doing this
btn = GameObject.FindObjectOfType(typeof(Button)) as Button;
btn.onClick.RemoveAllListeners();
btn.onClick.AddListener(SceneTransition);
This worked fine in the first scene (the Menu scene) but didn't on the Story scene. I used the Debug.Log() function to trace what was in EventSystem and notice that it was not changing to the new button in the Story scene.
So after some more head scratching I thought about doing this:
nextBtn = GameObject.Find ("Next");
nextBtn.GetComponent<Button> ().onClick.RemoveAllListeners ();
nextBtn.GetComponent<Button> ().onClick.AddListener (SceneTransition);
And now it works! So I first find the GameObject and find it's component, which is a button and remove any existing listeners then add the new one, simple right!?
Thanks to all that took the time to check out my question :)
Glad you solved the problem :) You could create an answer to your own question with the contents of your post update, and then accept it as the correct answer. This way, people will immediately see the question as "answered" :)
Answer by ekoguy · Mar 19, 2021 at 05:39 PM
Guys why I dont know but onclick just unworking in specific buttons. The brightness of button should change when mouse cursor hits the button but just didnt work. I wrote "button.getcomponent().enabled = true;" to void update too but unworking again. Please help me.