I've been struggling with this for the last couple of hours. I can access and edit the properties of the button, but the onClick.AddListener() function does not work. Here is my code:
GameObject gameObject;
gameObject = Resources.Load("Prefabs/MenuOption") as GameObject;
gameObject.GetComponent<Image>().sprite = Resources.Load<Sprite>(option.imgPath);
Button button = gameObject.GetComponent<Button>();
// Testing to see if I can change button properties (works)
ColorBlock colorss = button.colors;
colorss.highlightedColor = Color.magenta;
button.colors = colorss;
// This does not work
button.onClick.AddListener(() => Test(button));
button.onClick.AddListener(() => { Debug.Log("click"); });
Instantiate(gameObject, pos, new Quaternion(), GameObject.Find("Canvas").transform);
And here is an instance of the prefab: image
If anyone knows what I'm doing wrong, I would be very grateful.
Subscribing to onClick event on the instantiated game object (not on the game object you use to instantiate another object) should solve your problem.