x


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.

more ▼

asked Mar 29 '10 at 05:38 AM

Rufalo gravatar image

Rufalo
65 5 6 13

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first
var theArray : String[];

function OnGUI () {
    for (i = 0; i < theArray.Length; i++) {
        if (GUILayout.Button("String"+(i+1)))
            print (theArray[i]);
    }
}
more ▼

answered Mar 29 '10 at 06:04 AM

Eric5h5 gravatar image

Eric5h5
80.3k 42 132 521

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5099
x356
x104

asked: Mar 29 '10 at 05:38 AM

Seen: 2996 times

Last Updated: Mar 29 '10 at 06:09 AM