Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Created September 8, 2021 22:43
Show Gist options
  • Save SiarheiPilat/a5b95bc728afc322ec2c5069a4be7781 to your computer and use it in GitHub Desktop.
Save SiarheiPilat/a5b95bc728afc322ec2c5069a4be7781 to your computer and use it in GitHub Desktop.
Sets layer on all transforms and children
// taken from: https://forum.unity.com/threads/change-gameobject-layer-at-run-time-wont-apply-to-child.10091/
// special thanks to: chadfranklin47
public static class ExtensionMethods
{
public static void SetLayerRecursively(this Transform parent, int layer)
{
parent.gameObject.layer = layer;
for (int i = 0, count = parent.childCount; i < count; i++)
{
parent.GetChild(i).SetLayerRecursively(layer);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment