- Home /
Question by
gjdjgh5500 · Apr 12, 2016 at 10:17 AM ·
buttonarraylistonclick
Looping through Button Array and checking for OnClick
Hello,
I'm using the List collection for storing Buttons and iterating through this collection like so:
for (int i = 0; i < buttonList .Count; i++) {
buttonList [i].onClick.AddListener (() => myMethod(i));
}
What I am wanting to do is check every button to see if they have been clicked and get the appropriate value returned so I can get the right data from another method.
The problem is that when it increments through, the value for "i" is always at the maximum value.
Took a while but I figured it out. Heres the solution:
for(int i = 0; i < buttons.Length;i++ ) {
int capturedIterator = i;
buttons[i].onClick.AddListener(() => DoSomething(capturedIterator));
}
Found solution here: http://answers.unity3d.com/questions/930657/for-loop-with-ui-buttons-alternative-in-new-ui.html
Comment