I have been unable yet to add a working Button to a Canvas in Unity 4.6, using code. (C#)
Sure I’ve made a prefab using a Button - I use it later to make instances. However I just cannot get the onclick
going. Does someone have a complete example on how to do it?
The issue I’m seeing after instantiating is the onclick
does not fire what so ever.
All through code, or instantiating a prefab, whichever way is fine. The onclick
must be made in code, however.
This is how it looks more or less now:
GameObject gameobjectButton = (GameObject)Instantiate(this.gameobjectMjalertbutton);
Button buttonofbutton = gameobjectButton.GetComponent();
buttonofbutton.onClick.AddListener(delegate {
Debug.Log("FIRE!!");
});
gameobjectButton.transform.SetParent(gameobjectButtons.transform, false);
Now, the onclick just does not work/fire. The button is visible in the interface.