Creating Buttons from a Button created from an Array

Hi!

This makes no sense (At least for me! haha) The question is, I have a Listt of button I created with an array,everything is ok 'til here. I want each of those buttons to create more buttons from another array (I have an array called Section with all sections, and it has Name and Subsections, which is another Array with all subsections, this is what I want to show) The problem is, it works if I can show the subsections with a Debug.Log, and the button is working obviously, I can also hide the GUI or whatever, but not create any button!! and that’s what I need!, here is the Script.

var Side : Texture2D;
var Up : Texture2D;
var customGuiStyle : GUIStyle;
var AllSections : Section[];

function OnGUI(){
	GUI.DrawTexture(Rect(0,75,200,725),Side);
	GUI.DrawTexture(Rect(0,0,480,75),Up);
	for (var i=0;i<TodasSecciones.length;i++){
		if(GUI.Button(Rect(0,75+(i*50),200,50),""+AllSections*.Name, customGuiStyle)){*
  •  //This is working*
    
  •  	Debug.Log("Hi");*
    
  •  //But this is not*
    

*if(GUI.Button(Rect(200,75,200,50),“Hi”, customGuiStyle)){} *

for (var j=0;j<AllSections*.Subsections.Length;j++){*
//this is working too
Debug.Log(AllSections*.Subsections[j].Title);*

//but this is not
if(GUI.Button(Rect(200,75+(j50),200,50),“”+AllSections.Subsections[j].Title, customGuiStyle)){*
* }*
* }*
* }*
* }*

}
Help! Thank you!!!
:slight_smile:

GUI.Button() returns true only once when you click the button. Thats why you only get one output from Debug.Log("Hi"); for one press. That means also the second loop gets executed only that one time. You won’t necessarily even see the second buttons flash on the screen because OnGUI() is executed more often than the screen is rendered.

You have to make a state machine where the first button changes a variable, and based on that variable a second loop draws the buttons from the second array every time OnGUI() happens.

#pragma strict
var Side : Texture2D;
var Up : Texture2D;
var customGuiStyle : GUIStyle;
var AllSections : Section[];

var category : int = -1;

function OnGUI()
{
	GUI.DrawTexture(Rect(0,75,200,725),Side);
	GUI.DrawTexture(Rect(0,0,480,75),Up);

	for (var i=0;i<TodasSecciones.length;i++)
	{
		if(GUI.Button(Rect(0,75+(i*50),200,50),""+AllSections*.Name, customGuiStyle))*
  •  {*
    
  •  	category = i;*
    
  •  }*
    
  • }*

  • if (category >= 0)*

  • {*

  •  for (var j=0;j<AllSections[category].Subsections.Length;j++)*
    
  •  {*
    
  •  	//this is working too*
    
  •  	Debug.Log(AllSections[category].Subsections[j].Title);*
    
  •  	//but this is not*
    

_ if(GUI.Button(Rect(200,75+(j*50),200,50),“”+AllSections[category].Subsections[j].Title, customGuiStyle))_

  •  	{*
    
  •  	}*
    
  •  }*
    
  • }*
    }