Hello,
I am trying to dynamically create buttons at runtime based on UI options which are given to me through Scriptable Objects that currently hold custom functions. Each of these functions is passed through an interface that is passed in the code down below.
My problem is that the cardButton.onClick.AddListener doesn’t do anything.
Please help!
//Creates cards for construction, building and troop training
public GameObject CreateCard(UICardData cardData)
{
this.data = cardData;
GameObject cardPrefab = Resources.Load("UIActionCard", typeof(GameObject)) as GameObject;
GameObject card = GameObject.Instantiate(cardPrefab);
Image cardIcon = card.transform.Find("Button").GetComponent<Image>();
cardIcon.sprite = cardData.Image;
Button cardButton = card.transform.Find("Button").GetComponent<Button>();
//since it seemingly doesnt accept functions from non monobehavior classes, lets try this.
UIActionHandler uiCard = card.AddComponent<UIActionHandler>();
uiCard.data = cardData;
cardButton.onClick.AddListener(() => uiCard.TriggerCardAction());
return card;
}