Dynamically add listeners to multiple Buttons

I am using Unity 5.3 and I have the following piece of code that generates a list of Buttons, the idea is for the user to select a level and then play it:

void BuildLevelSelectList(){
	foreach (Level level in LevelList) {
		GameObject levelTemplateCopy = GameObject.Instantiate (levelTemplate);

        /* removed some code here that is just for layout purposes */

		Button button = levelTemplateCopy.GetComponent<Button> ();
		button.onClick.AddListener (() => {
			SelectLevel(level);
		});
	}
}

void SelectLevel(Level level){
	Debug.Log (level.Name);
}

The problem is that the Debug.Log (level.Name); always prints the name of the last level in the list for every Button I click on in the list.

I know I’m doing something wrong, can someone please point me in the right direction?

Thanks!

That’s because your assigning each button the enumerator of the foreach. I don’t know the technical details exactly, but the difference between foreach and for loop is the foreach uses an object that runs over the iterated list or array or whatever. It stays the same object it’s just returning the current content. you’re buttons get a pointer to that object, which at the end points to the last entry for all buttons.
either remove it with a for loop, or create a temporary variable which you assign to the buttons after assigning it the current value. that’s called closure.