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;

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 :slight_smile:

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.