Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active June 2, 2022 10:31
Show Gist options
  • Save unitycoder/f7ed03311b328fdbf1d5 to your computer and use it in GitHub Desktop.
Save unitycoder/f7ed03311b328fdbf1d5 to your computer and use it in GitHub Desktop.
Loop All Transform Children (not deep scan)
// loops only first level
foreach (Transform child in transform)
{
Debug.Log(child.name);
}
// loop all recursive https://forum.unity.com/threads/loop-through-all-children.53473/#post-340519
void TraverseHierarchy(Transform root)
{
for (Transform child in root) {
TraverseHierarchy(child);
}
}
// Best, loop all with getcomponentsinchildren http://answers.unity.com/answers/287672/view.html
Transform[] Children = GetComponentsInChildren<Transform>(includeInactive:true);
foreach (Transform child in Children) {
// whatever
}
// activate all parents starting from child
Transform rootParent = someChildTransform;
while (rootParent != null)
{
rootParent.gameObject.SetActive(true);
rootParent = rootParent.parent;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment