Generate Buttons from Array

Basically, I want to have a set of buttons generated based on a sting array (as in, the amount of buttons is the size of the array, and the text on each button is each item in the array), and (of course) have different stuff happen based on which buttons are pressed.

But I haven’t the slightest clue how to automate that. Could someone please point me in the right direction?

I found an answer here, but I only know C#, so I was unable to use it. If anyone wouldn’t mind translating that to C#, that works too.

The syntax is almost the same between C# and Javascript.

It’s pretty simple - loop through your array. Make a button for each item.

Assuming your array is called ‘MyArray’

In C#

   for(int x=0;x<MyArray.Length;x++)
   {
      if (GUI.Button(new Rect(0,x * 30,100,20),S))
      {
            DoSomething(x);
      }
   }

This draws one button for each item in the array. It bumps the ‘y’ coordinate of the button up by 30 for each button (and each button is 20 tall, which means you should have a 10-pixel gap between each one).