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?