I was trying to make a little game, and I came accross this problem.

The goal is simple. There are going to be $n$ number of buttons, and each one will change a value of one variable. For example, the Button with the number 2 would add 1 to list[2].

This is my code. Don't forget that the solution must be a generalized solution for any positive integer number $n$.

 {n = 10},
 Table[list[i] = i, {i, n}];
   buttons = Sequence @@ Table[Button[i, list[i]++], {i, n}]
   list[#] & /@ Range@n,

But whenever I click a button, it throws me an error saying that "list[i] is not a variable with a value, so it's value cannot be changed", it's like when I call it in Table the variable sticks with list[i] instead of list[2](for example).

I tried to put Evaluate[] on almost everything to see if things were getting better with no luck, now I don't know how to deal with this.

  • 1
    $\begingroup$ Button has HoldRest attribute so you need to proceed like in case of injecting to dynamic, e.g.: ...Table[With[{i = i}, Button[i, list[i]++]],... $\endgroup$
    – Kuba
    Apr 28 '14 at 23:32
  • 1
    $\begingroup$ Related: Button's command is not evaluated in iteration. $\endgroup$
    – Artes
    Apr 28 '14 at 23:37