c# - Passing parameters to a listener through a delegate inside a loop doesn't seem to work -
this question has answer here:
- captured variable in loop in c# 6 answers
- unrolled loop works, loop not work [duplicate] 1 answer
i'm trying add listener bunch of buttons have in array. function i'd call receives gameobject , original onclick listener doesn't, pass gameobject function playerbuttonclickedhandler wrapping call in delegate:
for (int = 0; < characterbuttons.length; ++i) { characterbuttons [i].onclick.addlistener (delegate() { playerbuttonclickedhandler (characters [i]); }); }
that's advice got here: https://forum.unity3d.com/threads/how-to-addlistener-featuring-an-argument.266934/
but doesn't work, i've been debugging , i'm initializing listeners passing characters[8].
and following working, ugly hell:
characterbuttons [0].onclick.addlistener (delegate() { playerbuttonclickedhandler (characters [0]); }); characterbuttons [1].onclick.addlistener (delegate() { playerbuttonclickedhandler (characters [1]); }); characterbuttons [2].onclick.addlistener (delegate() { playerbuttonclickedhandler (characters [2]); }); characterbuttons [3].onclick.addlistener (delegate() { playerbuttonclickedhandler (characters [3]); }); characterbuttons [4].onclick.addlistener (delegate() { playerbuttonclickedhandler (characters [4]); }); characterbuttons [5].onclick.addlistener (delegate() { playerbuttonclickedhandler (characters [5]); }); characterbuttons [6].onclick.addlistener (delegate() { playerbuttonclickedhandler (characters [6]); }); characterbuttons [7].onclick.addlistener (delegate() { playerbuttonclickedhandler (characters [7]); });
why???? :(
Comments
Post a Comment