toggle add listener through script
I'm trying to retrieve a bunch of toggles and run a function when theyre value is changed. I've looked all over the web for at least 5 hours today and I'm about to go crazy. Im starting to get some serious tunnel vision so a fresh set of eyes would really help me out here.
Here's my code :
public void AddListenersToToggles(){
ElementComponents = obj.GetComponentsInChildren (typeof(Toggle));
foreach (Toggle _Toggle in ElementComponents) {
Debug.Log ("Found a toggle");
//_Toggle.onValueChanged.AddListener ();
_Toggle.onValueChanged.AddListener ((bool On) => {test(On);});
}
}
public void test(bool On){
Debug.Log ("FINALLY");
}
Zero success with that code...
For some reason though, if I use :
_Toggle.onValueChanged.AddListener ((bool On) => {Debug.log("FINALLY");});
...It runs the method and works as expected!
What am I missing here?!
I'm using 5.3.5f1
Thanks, -Andrew
Your code work fine for me.
I am using unity 5.4.2f2
the only tip i could give you is to restart unity. whenever something doesn't make any sense the first thing i try is to close unity and open it again.
Yonatan
Thanks for the confirmation, I tried it in a seperate class and it worked for me too. I've no idea what I'm doing wrong. The EXACT same code doesn't work for me in the class I'm trying to do it in.