x


How can I set up a series of OnGUI Toggle buttons in a for loop?

I loop through all the children components inside a GO.

go = GameObject.Find(target.gameObject.name);
comps = go.GetComponentsInChildren(Transform);

for (var  comp : Transform in comps) {

}

What i want to do is to create a GUI.Toggle for each GO's children inside the loop.

Is that possible?

Thank you in advance.

more ▼

asked Mar 09 '10 at 11:17 PM

chchrist gravatar image

chchrist
116 5 7 14

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

1 answer: sort voted first

Unless the children of the gameobject can change during runtime, I'd recommend getting and storing the list of children in Start(), then looping through the array in your OnGUI method.

var target : Transform;
private var children : Component[];
private var toggles : boolean[];

function Start() {
    children = target.GetComponentsInChildren(Transform);
    toggles = new boolean[children.Length];
}

function OnGUI() {
    for (var n=0; n<children.Length; ++n) {
        toggleRect = Rect(0,n*20,100,20);
        toggles[n] = GUI.Toggle(toggleRect , toggles[n], children[n].name);  
    }
}

(excuse the hard-coded rect values there - you'll probably want to modify those!)

more ▼

answered Mar 10 '10 at 08:21 PM

duck gravatar image

duck ♦♦
40.9k 92 148 415

Thank you!! I wonder if the GUI.Toggle has a callback function. Cause now i want to toggle the texture of each children.

Mar 11 '10 at 11:35 AM chchrist
(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:

x5059
x3673
x294
x159
x104

asked: Mar 09 '10 at 11:17 PM

Seen: 2096 times

Last Updated: Mar 10 '10 at 08:22 PM