c# - Passing parameters to a listener through a delegate inside a loop doesn't seem to work -


this question has answer here:

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

Popular posts from this blog

php - Vagrant up error - Uncaught Reflection Exception: Class DOMDocument does not exist -

vue.js - Create hooks for automated testing -

Add new key value to json node in java -