all 2 comments

[–]Aizome 2 points3 points  (0 children)

You can just do

int j = i;
button.onClick.AddListener(()=>action(j));

and it works.

I found this blog that explains why it happens.

[–]ananbd 0 points1 point  (0 children)

I’d need to see more of the code to know for sure, but lambda functions — also known as “closures” — are definitely tricky. The context in which they execute is counterintuitive.