How to stop clones sharing listener?

Unity 4.6, scripting the buttons… Iterate through a directory and create a button for each file… The value of f.Name in the AddListener bit should be set independently for each button. Instead every button has the value of the last f.Name to be processed (the last file alphabetically in the directory). Any ideas?

foreach (FileInfo f in fileInfo) {
	Button btnCurrLvl = (Button) Instantiate(btnLvl);

	btnCurrLvl.GetComponentInChildren<Text>().text = f.Name.Remove(f.Name.Length-5);

	Debug.Log(f.Name); // f.Name is different every time = f.Name; // renaming the btns works
	btnCurrLvl.GetComponent<Button>().onClick.AddListener(() => LoadLocalLvl(f.Name)); // all the listeners on all created buttons are set to the last value of f.Name!?

Shameless self promotion: I’ve got a video tutorial here on this.

This is the nature of loops and variable scopes. To fix you simply need to declare a local scope variable.

string tempName = f.Name;
btnCurrLvl.GetComponent<Button>().onClick.AddListener(() => LoadLocalLvl(tempName));