Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unrolled Loop works, for loop does not work [duplicate]

I have some behaviour that I do not understand. While the unrolled loop works fine!!! The loop throws IndexOutOfRangeExceptions. Debugging shows that there 0..9 teamButtons and 0..9 cards c[i]. :(

private void Awake()
{
    InitCards();
    // This works!
    teamButtons[0].onClick.AddListener(() => SetCard(c[0]));
    teamButtons[1].onClick.AddListener(() => SetCard(c[1]));
    teamButtons[2].onClick.AddListener(() => SetCard(c[2]));

    teamButtons[3].onClick.AddListener(() => SetCard(c[3]));
    teamButtons[4].onClick.AddListener(() => SetCard(c[4]));
    teamButtons[5].onClick.AddListener(() => SetCard(c[5]));

    teamButtons[6].onClick.AddListener(() => SetCard(c[6]));
    teamButtons[7].onClick.AddListener(() => SetCard(c[7]));
    teamButtons[8].onClick.AddListener(() => SetCard(c[8]));
    // This yields an IndexOutOfRangeException
    for (int i = 0; i < 9; ++i)
    {
      teamButtons[i].onClick.AddListener(() => { SetCard(c[i]); });
    } 
}
like image 758
Rofl Ukulus Avatar asked May 03 '26 09:05

Rofl Ukulus


1 Answers

You're capturing the variable i in your lambda expression. When that lambda expression is executed, it will use the "current" value of i - which will always be 9. You want to capture a copy of the variable... which you can do be introducing a new variable in the loop:

for (int i = 0; i < teamButtons.Length; i++)
{
    int index = i;
    teamButtons[i].onClick.AddListener(() => SetCard(c[index]));
} 
like image 85
Jon Skeet Avatar answered May 04 '26 23:05

Jon Skeet



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!