UI Toggle onValueChanged.AddListener in List of Toggle

Hi,

I trying to add event to a list of Toggle component, when I click on toggle he return always the last toggle in list:

foreach (Toggle go in desertToogleList) { 
				go.onValueChanged.AddListener((value) => ToggleClicked(go.name));
			}

private void ToggleClicked (string toggleNo) {
		if(UtilsManager.GetDebug()){Debug.Log ("Toggle changed = " + toggleNo);}
	}

How I can add event to List of Toggle properly ? 

Thanks

Try this:

foreach (Toggle go in desertToogleList) {
    Toggle t = go;
    t.onValueChanged.AddListener((value) => ToggleClicked(t.name));
}

I guess C# uses the same variable (go) for each iteration, so the lambdas end up pointing to the last element of the list. When you create another variable, it stores the reference to the current element of the list, so the lambdas point to that object.