I am trying to set the layer to look for during runtime, and I can get it to work just fine if I expose it in the inspector and select a LayerMask from the dropdown. But I want to know how I can do it without picking it in the inspector.
Why wouldnt I just make a static class with a GetMask() method then? seems like a lot more work making it an SO I have to drag every time, would be the same as exposing a field in the inspector.
LayerMasks in Unity are implemented as a bitmask. I have an essay of a post explaining what bitmasks are here , if you’re unfamiliar. @craig4android 's solution was technically correct. 1 << LayerMask.NameToLayer( "enemy" ) will generate the appropriate bitmask for use with physics. If that snippet didn’t work for you, perhaps there is something else that is causing it not to work. The LayerMask.GetMask method does the same thing and is interchangeable with the above method.
Check to make sure that the objects you are colliding with have the correct layer, and that the sphere is as large you want and in the correct place.
Static class will not be visible in an editor, you would have to create enum.
But thanks to my method.
You would be able to add new masks without editing the code.
So if you have another people working on your project this workflow will be a lot of smoother.
I went that way never looked back.