It prints 3
because you're not boxing the value of i
, it's value is getting re-written each time it iterates.
To fix this try doing something like this :
for ( int i = 0; i < options.Length; ++i )
{
int j = i;
options[i].onClick.AddListener( () => { OptionPressed(j); } );
}
Check the difference