I'm trying to do a Candy Crush-like map for my game in Unity, I'm trying to gather all the buttons in an array and then set its onclick property so when I click on them I get to the level I want. I'm using a Foreach loop to achieve it.
The problem is that on every single button, I get to load the same scene. It does list my levels from 1 to N but the onclick does not seem to be working fine! I've tried doing a for loop instead foreach but it doesn't work either.
int i = 0;
foreach(GameObject level in levels)
{
level.GetComponent<Button>().onClick.AddListener(() => SceneManager.LoadScene(i + 1));
i++;
}