I have a GUI setup code in my game, like:
for (int i = 0; i < count; i++)
{
...
Button button = new Button();
...
button.handler = delegate { selectedIndex = i; };
gui.Add(button);
...
}
I want to make button changing selectedIndex to current value of i, that was on it's creation. I.e. button0 changes it to 0, button1 to 1 and so on. But it looks like it dinamycally link value in delegate to i variable and all buttons changes selectedIndex to count + 1.
How to fix it?