Unrolled Loop works, for loop does not work

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]); });
    } 
}
Jon Skeet
people
quotationmark

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]));
} 

people

See more on this question at Stackoverflow