- Home /
Trying to get an array to make working buttons. [Script Update]
Ok, so i have this array that makes some GUI buttons and adds the right text to them. my problem is that i don't know how to get the functionality behind them using another array. here is what i have working and the beginning of what i thought would work:
var theButtonNameArray = new Array ("Start", "Resume", "Options"); var theButtonFunctionAction = new Array ("Level1", "Level1", "OptionsScreen");
function OnGUI () {
// Make a text field that modifies stringToEdit.
for (i = 0; i < theButtonNameArray.length; i++){
if (GUI.Button (Rect ((Screen.width*.5)-(theButtonPositionWidth*.5), (Screen.height*.5)-(theButtonPositionHeight+theButtonSpace)*(i*3), theButtonWidth, theButtonHeight), theButtonNameArray[i] + theButtonFunctionAction[i]));
{
Application.LoadLevel(theButtonFunctionAction);
/*print("Loading Level!");*/
}
}
}
this should be correct right? i'm still getting an error on the Application.LoadLevel or the print line saying: BCE0044: expecting :, found ';'.
FIXED IT! i'm such a noob. i had a ";" on the end of my if statement.
i've found out that using an enum ins$$anonymous$$d of an array would work better. with the array it wouldn't update the counters, but the enum will.
Answer by by0log1c · Mar 09, 2011 at 10:45 PM
Ahh, I've been trying to do something kind-of similar lately....actually not that much but oh well, I see what you're trying to obtain. I'm sure there's a 'clean'er way to do it, by storing 'function call'(is that what Deleguates are for???) or something.
An alternative is to store numerical value, instead of the methods themselves. Then switch() the lastButtonValue and call the desired functions.
OR - not recommended for obscure reasons, yet that could work: store the functions name as Strings in the second array then use eval().
var buttonNames = new Array("myButton1","myButton2","myButton3"); var buttonValues = new Array("myFunc1","myFunc2","myFunc3");
for(i=0;i<buttonNames.length;i++){ if(/userPressButton[i]/){ eval(buttonValues[i]+"();"); } }
function myFunc1(){//user clicked myButton1; } function myFunc2(){//user clicked myButton2; } function myFunc3(){//user clicked myButton3; }
While purely theoretical, I guess this should/could works and create a button for every buttonNames and once clicked. eval() should transform the Strings+() from the array into:
myFunc[i]();
Answer by DaveA · Mar 09, 2011 at 10:06 PM
Try these: GUI Toolbar: http://unity3d.com/support/documentation/ScriptReference/GUI.Toolbar.html GUI Grouping: http://unity3d.com/support/documentation/ScriptReference/GUI.BeginGroup.html GUI Layout has similar things, including a grid: http://unity3d.com/support/documentation/ScriptReference/GUILayout.SelectionGrid.html
And there's probably other ways of doing it too.
Your answer
Follow this Question
Related Questions
sort gui button 0 Answers
changing GUI Button text with a string array 2 Answers
Create GUI based on an array 2 Answers