Hi
I have this code to add onclick listener and method call to button in prefab.
Everything else works in this code, except onclick listener and method call. With this code, i get red button with text “labelText” and it clicks visually. It does not run myMethod and while running, checking from inspector it does not have onclicks.
putikka = item.GetComponent<Button>();
if (putikka != null)
{
Debug.Log("Button found");
putikka.GetComponent<Button>().onClick.AddListener(() => { myMethod(data); });
putikka.GetComponentInChildren<Text>().text = "labelText";
putikka.GetComponent<Button>().image.color = Color.red;
putikka.GetComponent<Button>().interactable = true;
}
This is alternative line i have tried with similar success…
putikka.GetComponent<Button>().onClick.AddListener(delegate () { myMethod(data); });
So i can clearly access the button cause color and interactble can be changed. It even dont give me any errors to help solve the problem.
What am i doing wrong?