- Home /
How To Inactivate Childs of a GameObject (GUI)
Hi, I'm new to Unity and i try to create a menu by using the new GUI (4.6). I want a menu looking like the KeyNote menu, with a panel to the right of the scene which changes when we click one of the buttons.
All my panels are in a EmptyGameObject. If i inactivate this GameObject i can't activate a specific panel after. My idea was :
Inactivate all the panels to make sure any pre-existent panels are deactivated
Activate the panel linked to the button (with SetActive)
I do not managed to inactivate all the children of the GameObject, any solutions ? If u have a better idea on how to make this kind of menu, feel free to contribute.
Thanks. Please excuse my poor English
If all the panels are the same height, width, and position, (and the only difference is the contents), you might consider swapping out contents ins$$anonymous$$d.
In either case, I think your solution is fine:
OnClick any button...
Disable all panels...
Enable the correct panel.
Create a Panel$$anonymous$$anager script which has a collection of panel (or panel_content) objects. Iterate over this collection to disable all panels. The buttons could pass an integer index to a method on the manager to select the correct panel from the collection.
In what way is this not working out for you?
Thanks, i did it this way !
I thought to use the same method but with one panel and several GameObject containing contents, so it will swap out content ins$$anonymous$$d of swapping panels.
Answer by fffMalzbier · Mar 11, 2015 at 03:15 PM
You can get the children of your EmptyGameObject and deactivate them one by one. After that you can activate the one you like to have active.
for (int i = 0; i < transform.childCount; i ++)
{
transform.GetChild(i).gameObject.SetActive(false);
}
Answer by Mol · Mar 11, 2015 at 04:07 PM
Thanks ! I did something similar and it's working fine.
public void ActivatePanel(string panelId)
{
foreach(Transform child in transform)
{
if(child.name == panelId)
{
child.gameObject.SetActive(true);
}
else{
child.gameObject.SetActive(false);
}
}
}