Skip to content

Instantly share code, notes, and snippets.

@unitycoder
Last active June 9, 2024 10:22
Show Gist options
  • Save unitycoder/65f604895f1699b92af1ba7762bfab45 to your computer and use it in GitHub Desktop.
Save unitycoder/65f604895f1699b92af1ba7762bfab45 to your computer and use it in GitHub Desktop.
Check if Layer Exists (by name), Assign Gameobject to layer using LayerMask
// assign gameobject layer using layermask NOTE returns -2147483648 if you havent set the layermask in inspector!
go.layer = (int)(Mathf.Log((uint)layermask.value, 2));
// set layers to children also
foreach (Transform t in go.transform)
{
t.gameObject.layer = (int)(Mathf.Log((uint)layermask.value, 2));
}
// check if layer exists by name
var newLayer = LayerMask.NameToLayer("MyLayer");
if (newLayer > -1)
{
// we have it
}
@unitycoder
Copy link
Author

Check if GameObject is in Layer or check if Layer is in LayerMask
https://gist.github.com/unitycoder/cb887da3be89458968101a92cf61720b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment