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

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

Leave a Comment