Dynamic gui buttons

I am setting up a list of buttons to be generated at runtime based on data in the game.

It looks like

for(i=0;i<someList.length;i++)
{
         if(GUI.Button(Rect (0, i*30, 90, 30), someList[i]))
	{
                 print("you pressed button " + ?);
        }
}

When a button is clicked, how can I work out which button it was? The system is a little counterintuitive in that the whole loop is processed before any buttons can be pressed, therefore the counter variable i is equal to someList.length and not the index of the button that was clicked. I tried using GUI.SetNextControlName (); but when I press a button the GUI.GetNameOfFocusedControl () function returns an empty string. I am not out of ideas as the documentation for events and controls doesn’t mention any queries that can be made as to the control that was clicked.

What should I replace the ? with

Did you actually try to put i there? I think it would work.

I’ve create GUI buttons in loops before, and if worked fine. Are you saying that:

print("you pressed button " + i);

Always prints out the value of someList.length regardless of which button is clicked?

ok it appears the array index out of bounds exception was not caused by this at all. I apologise for asking the wrong question. The real problem I am having is that if I make a button that makes another button I don’t see the second one.

if(GUI.Button(Rect (100, 100, 300, 300), "test"))
{
	if(GUI.Button(Rect (400, 100, 90, 30), "test2"))
	{
		print("yay");
	}
}

The button test2 does not appear when I click test. Something I read in the reference docs lead me to believe this should work

var myBool = false;

if((!myBool)
{
     if(GUI.Button(Rect (100, 100, 300, 300), "test"))
     {
          myBool = true;
     }
}
if((myBool)
{
        if(GUI.Button(Rect (400, 100, 90, 30), "test2"))
	{
		print("yay");
	}
}

What appels said… GUI buttons only return true for the frame that they are clicked, so you’ll need to use GUI buttons to set boolean vars, and show/hide the buttons you want other buttons to enable with the booleans.

Thanks a lot guys, that worked.

for(i=0;i<someList.length;i++)

{

if(GUI.Button(Rect (0, i30, 90, 30), someList))*
{
print("you pressed button " + someList*);*
}
}

Dynamic gui buttons