Вот портянка моего кода:
public class FoodPanel : MonoBehaviour
{
public GameObject FoodTypePrefab;
public GameDataManager GDM;
public Transform foodPanel;
public GameObject breadCard;
public GameObject meatCard;
public GameObject cakeCard;
private void Start()
{
GDM = GameObject.Find("GameManager").GetComponent<GameDataManager>();
breadCard = CreateFoodCard("Bread",GDM.gameData.level1Data.resourcesLvl1.bread,FakeClickHandler);
meatCard = CreateFoodCard("Meat", GDM.gameData.level1Data.resourcesLvl1.meat,FakeClickHandler);
cakeCard = CreateFoodCard("Cake", GDM.gameData.level1Data.resourcesLvl1.cake,FakeClickHandler);
}
public GameObject CreateFoodCard(string labelText,int foodData,UnityAction clickHandler)
{
GameObject newFoodCard = Instantiate(FoodTypePrefab,foodPanel);
TextMeshProUGUI cardLabel = newFoodCard.GetComponentInChildren<TextMeshProUGUI>();
cardLabel.text = labelText +" " + foodData;
Button cardBtn = newFoodCard.GetComponentInChildren<Button>();
Debug.Log(cardBtn);
cardBtn.onClick.AddListener(clickHandler);
return newFoodCard;
}
void FakeClickHandler()
{
Debug.Log("Food card clicked");
}
Проблема пришла откуда не ждал - не навешивается обработчик клика. Код вроде элементарный, совершенно не понимаю в чем может быть проблема. Никаких предупреждений/проблем в консоли нет.