- Home /
Display buttons based on the amount of children
I want to show a GUI with a number of buttons depending on the amount of children the selected GameObject has. Each button should show a Texture2D image that is assigned as a variable via a script (Obj_Properties) inside each children. I`ve been trying to figure out how to do this but I don't think I have the knowledge to do so...
This is what I wrote so far for the GUIOn and it just doesn't seem to work:
//number of children in Object.
var numberOfItems : int = Player_Navigation.selectedObject.childCount;
//number of children in Object that contain the Obj_Properties script (where I get the thumbnail image)
var childrenInObject = Player_Navigation.selectedObject.GetComponentsInChildren(Obj_Properties);
var yOffset = 0;
GUILayout.BeginArea(Rect (25,25,150,300));
GUILayout.BeginVertical();
for(i in childrenInObject){
if( GUI.Button (Rect ( 30, 30 + yOffset, 30, 43), GUIContent("", childrenInObject[i].GetComponent(Obj_Properties).objThumbnail))){
Debug.Log("something :" + i );
}
yOffset += 30;
}
GUILayout.EndVertical();
GUILayout.EndArea();
Any help very welcome!
Answer by whydoidoit · Sep 28, 2012 at 08:29 PM
You are nearly there! Because you are using in the if line should read:
if( GUI.Button (Rect ( 30, 30 + yOffset, 30, 43), GUIContent("", i.objThumbnail))){
It might complain about objThumbnail not existing in JS depending on whether you have #pragma strict - if it does you can use (i as OBJ_Properties).objThumbnail
Super fast reply and it works!! (I added the 'GetComponent(Obj_Properties)' so it doesn't complain.
But now I realized that this way I also get the parent in the group, what would be the best way to ignore it from that list?
I have a boolean inside Obj_Properties that says if the object is parented or not. $$anonymous$$aybe its something I would use here?
Easiest way would be an if in the loop.
if(i != Player_Navigation.selectedObject)
{
//Loop body
}
Oh hell - you probably want the OBJ_Properties from that - damn.
var parent = Player_Navigation.selectedObject.GetComponent(OBJ_Properties);
for...
if(i != parent)