S
S
Sergey Bezrukov2018-09-04 07:31:06
C++ / C#
Sergey Bezrukov, 2018-09-04 07:31:06

Why is the parameter not passed through onClick.AddListener?

There is a piece of code:

for (int i = 0; i < achieveList.Count; i++)
{
    test[i].GetComponent<Button>().onClick.AddListener(delegate { openDescr(i); } );
}

In the Button component of each test object, the onClick parameter must be a number equal to i at the time the listener is assigned. But it turns out that each button has a parameter equal to the size of the array, that is, 1 more than its last element. What can be wrong.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Pupkin, 2018-09-04
@Hrederik

Closures! Heaps of information.
Here for example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question