- Home /
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.
Answer by duck · Mar 10, 2010 at 08:21 PM
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!)
Thank you!! I wonder if the GUI.Toggle has a callback function. Cause now i want to toggle the texture of each children.