I am using the new Unity3d 4.6 GUI Beta. I have created a canvas and attached a button to the canvas. I have attached a script to an empty game object with a method in it to be used by the button. When I click the button everything works fine. However, if I turn the same canvas into a prefab, when I instantiate the prefab the "On Click" portion of the button becomes empty and the button no longer functions. I also tried making just the button a prefab instead of the canvas with the button as its child and it was the same thing. I am really hoping someone knows how to make a button prefab where the button still works.
3
votes
1 Answers
2
votes
You can get around this like I did - I ended up adding the button listener manually in code (see below). Just make sure this is executed when your prefab is instantiated. The "UI way" to do this that you were trying seems broken even in the latest Unity client.
var buttonTransform = this.transform.FindChild("Button");
_buttonScript = buttonTransform.GetComponent<Button>();
_buttonScript.onClick.AddListener(() => Flip());
There is more info on this issue on the Unity site, http://answers.unity3d.com/questions/794720/on-click-paramaters-disappear-from-button-prefab.html but I couldn't find a corresponding issue logged with their team.