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