Hi,
I have some issues with adding events to EventTrigger on runtime.
Here is my code:
int i = 0;
foreach(Transform x in myGameObject)
{Debug.Log(i);
EventTrigger.Entry entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerEnter;
entry.callback.AddListener((eventData) => {MyFunction(i);});
x.GetComponent<EventTrigger>().triggers.Add(entry);
i++;
}
public void MyFunction(int x)
{
Debug.Log(x);
}
The problem is that when i point at any UI object which was set-up this way Debug in MyFunction returns 10 (number of childs in myGameObject) but Debug in foreach shows all numbers from 0 to 9.
Does anyone know why this happens and what should I do to make my UI objects to call MyFunction with the variable I give them?