- Home /
Why defining a public LayerMask variable doesn't translate the same as defining a private where you use NameToLayer to select the correct layer?
I had a public variables in my script where I could select from Unity which Layer corresponded to the variable.
I decided then to switch the control to code, with a const string defining the LayerName, and a LayerMask variable to be determined on Awake() (though I moved the definitions to Start() and Update() as well, just to see if it'd fix the problem).
In a nutshell, here's what the code would look like.
public const string grabLayer = "Grabable"; // outside of any functions
private LayerMask grabLayer; // outside of any functions
grabLayer = LayerMask.NameToLayer(grabLayer);// in Awake() or Start() or Update()
scanForItem = Physics2D.Raycast(transform.position + Vector3.up, Vector3.right * transform.localScale.x, grabbingDistance, grabLayer); // in Update()
I have the same issue with a Phyisics2D.OverlapCircle. When I write to the console the LayerMask index it properly shows the value for the same layer I was choosing from the inspector when the variable was public.
Is there anything in particular in the way the Physics2D class treats layers that would make it treat public / private LayerMasks differently?
Answer by Bunny83 · Apr 30, 2017 at 06:51 PM
The LayerMask.NameToLayer method returns the "index" of the layer with that name, not a layermask. A LayerMask is a bitmask. You can turn an index into a vitmask with only that layer by using the left-shift operator like this:
layerMask = 1 << IndexOfLayer;
For example an index of "10" would lead to a mask like this: "0x00000200" or in binary:
00000000 00000000 00000010 00000000
Answer by talyh · May 01, 2017 at 09:08 AM
Bunny83's pointed me in the right direction to further search the forum and get a better understanding of layer index x layer mask. For others searching, the following thread was really useful. http://answers.unity3d.com/questions/8715/how-do-i-use-layermasks.html