Hello! I had some issues adding events with parameters to a unity UI button in a cycle, my initial code was:
for( int starN = 0 ; starN < _missionStars.Length ; starN++)
{
Button missionButton=_missionStars[starN].gameObject.AddComponent<Button>();
missionButton.onClick.AddListener(delegate{OnPressNew([B]starN[/B]);});
}
this didn’t work correctly - each button had the same value ( equal to _missionStars.Length)
after much consternation, I finally got it to work by adding this line:
for( int starN = 0 ; starN < _missionStars.Length ; starN++)
{
Button missionButton=_missionStars[starN].gameObject.AddComponent<Button>();
int misisonIndex=starN;
missionButton.onClick.AddListener(delegate{OnPressNew(misisonIndex);});
}
is this a bug in unity or am I unaware of some programming concept/intricacy?