- Home /
LayerMask is incorrect
LayerMasks are a natural and easy tool to use. They set a bit for each other mask you specified. For example, I specified two layers in the inspector B, C in the inspector. Thus, the bits set if I have layers A, B, C, D should be 0110. My layerMask variable should EQUAL this value.
It doesn't.
In my case I have about 12 custom layers, all layers start at layer 8 (Unity's built in layers are before that). I should see two bits set in the int because I specified B and C. Instead the resulting bits are as follows:
0001 0011 1001 0010 0110 0100
Note that after the first 7 bits (right side) you see 6 bits set.
Why? Any ideas? This clearly is wrong, but the inspector clearly tells me otherwise. I'm not changing this variable at runtime either.
I don't think there's a problem with how Unity stores or evaluates layermasks (I've certainly never experienced a problem with them, and nor have I ever seen such a problem reported). I'd guess that it's a problem with the code that you're using to test - can you post an example script/repro project that demonstrates your layer setup, and how you're deriving that 001 0011 1001.... value above?
Well. Apparently Windows 10's calculator defaults to hex and I typed my decimal number into it. That would certainly do it. I confirmed that the bits ARE set properly.
Sorry about that.
Answer by CJ-Clark · Jul 03, 2017 at 01:15 AM
Confirmed it was user error. I converted my layerMask.value to binary but Windows 10's calculator apparently defaulted to hex, causing me to type in a hex value instead of decimal...