In my application I have a list of GameObject's, and I'm creating a button for each GameObject in the list via code. The problem is that when I add and onClick Event via code nothing shows up in the button onClick list, nothing happens when I click the button and I get no errors in the process. Here is how my code looks like:
public GameObject prefab;
public void Generate()
{
for (int i = 0; i < myList.Count; i++)
{
GameObject _t = Instantiate(prefab, myUIPanel.transform) as GameObject;
//Positioning, naming, ...
_t.GetComponent<Button>().onClick.AddListener(delegate { MyFunction(i); });
}
}
public void MyFunction(int index)
{
//...
}
I've created a GUILayout.Button inside an editor script to call the "Generate" method. The buttons are created and I get no errors, but no Event is added in the buttons.