- Home /
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[i].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[i].Subsections.Length;j++){
//this is working too
Debug.Log(AllSections[i].Subsections[j].Title);
//but this is not
if(GUI.Button(Rect(200,75+(j*50),200,50),""+AllSections[i].Subsections[j].Title, customGuiStyle)){
}
}
}
}
}
Help! Thank you!!! :)
Answer by NoseKills · Jun 05, 2014 at 06:15 PM
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[i].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))
{
}
}
}
}
Thank you!! I cant vote positive because I dont have 15 reputation :( srry... But thank you though!!
I think you should be able to select this as the "answer". There should be a tick mark somewhere... next to the thumbs maybe
Your answer
Follow this Question
Related Questions
Read Array Inside Array 0 Answers
How to set parents of all objects in an array? 1 Answer
Array index is out of range 1 Answer
How to return index of List element? 1 Answer