- Home /
Question by
Shayan3Dkaraj · Aug 07, 2019 at 12:05 PM ·
editorhierarchyparent-child
Activate parent and its children at the same time
Hello everyone, I have a GameObject with 3 children in the Hierarchy window. When I deactivate cildren's parent , children of gameobject are not be deactivate but I want to deactivate or activate parent and its children at the same time.
Comment
Best Answer
Answer by georgelewising · Aug 07, 2019 at 12:33 PM
We'd call that recursion! Use this wherever you want:
public static void SetActiveRecursively( Transform parent, bool active )
{
parent.gameObject.SetActive(active);
foreach (Transform child in parent)
{
SetActiveRecursively(child, active);
}
}
Answer by Shayan3Dkaraj · Aug 07, 2019 at 03:14 PM
@georgelewising thanks for your answer , but what's my mistake in this script:
Public GameObject cube1;
cube1.set active(false); //cube1 is parent and has 3 children
SetActiveRecursively();
But when cube1 = false , its children are true and active!
Hey! You can reply to answers as comments!
Turn everything off: SetActiveRecursively( cube1.transform, false);
Turn everything on: SetActiveRecursively( cube1.transform, true);
Your answer
![](https://koobas.hobune.stream/wayback/20220612210209im_/https://answers.unity.com/themes/thub/images/avi.jpg)