- Home /
List the Index of an Array as Buttons
Hi, I'm trying to build a GUI list of an array.
I have this array which has 3 values: String 1, String 2, String 3.
I want to be able to list all those values like:
String 1
String 2
String 3
and if I click on them, I get the location of it in the array. If I click String 1, I get array[0].
I think it's possible to build this with a for loop and build a GUI button for every item in the array, but I'm not sure how to create new buttons and assign them to the array's object.
Is there another way to do this or are the buttons the best method?
And of course I want the buttons to update if I add to the array.
Answer by Eric5h5 · Mar 29, 2010 at 06:04 AM
var theArray : String[];
function OnGUI () { for (i = 0; i < theArray.Length; i++) { if (GUILayout.Button("String"+(i+1))) print (theArray[i]); } }
C#
public string[] theArray;
void OnGUI() {
for( int i = 0; i < theArray.Length; i++ ) {
if( GUILayout.Button( "String" + (i+1) ) )
print( theArray[i] );
};
}