- Home /
Question by
CommandoXel · Jan 02, 2014 at 03:17 AM ·
javascriptparent and child
Parent child object swapping, javascript with GUI
Hi, I was wondering how I can swap a few game objects around with the parent/child method using javascript, while also using GUI to change the game objects around.
For example, I have a structure with a sphere, cylinder and a cube, and I was wondering how would I script it with javascript using GUI buttons to switch between the three objects?
Comment
Answer by sparkzbarca · Jan 02, 2014 at 03:35 AM
little confused what you want
this might help
//fill ObjectList with the sphere, cylinder and cube
List<GameObject> ObjectList;
//this will be the a list of the names of each stored as a gui content to be
//displayed on each button
List<GUIContent> BaseEditorObjectsList;
void Start()
{
foreach(GameObject RawObject in ObjectsList)
{
Temp = RawObject;
BaseEditorObjectsList.Add(new GUIContent(Temp.name));
}
Width = 100 * BaseEditorObjectsList.Count;
Height = 30;
ListScreenRect = new Rect(0,0,Width,Height);
}
// Update is called once per frame
void OnGUI()
{
SelectedSpawnObject = GUI.SelectionGrid(ListScreenRect,SelectedSpawnObject,BaseEditorObjectsList.ToArray(),BaseEditorObjectsList.Count);
}
//this will give you a function to retrieve the selected object
public GameObject SpawnObject()
{
return (GameObject) ObjectsList[SelectedSpawnObject];
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613125326im_/https://answers.unity.com/themes/thub/images/avi.jpg)