List has strange values at run time

I’m attempting to make a public List placeableLayers in my script where I can choose the layers I want to be in the list via the inspector.

I set up my list in the inspector like normal (I can choose from the list of available layermasks and put them in the list), but when the code runs and compares the LayerMask’s ‘value’ field against a gameObject.layer item, the LayerMask’s value (from the List) has bizarre out of bounds layermask values, like 2048 and 4098 (powers of 2, interestingly enough).

Am I misunderstanding how to compare layer masks?

The code that does the comparison:

// placeableLayerMasks is a List<LayerMask> declared public for this class, filled in via the inspector with valid LayerMasks.
// is this object configured with a layer that matches the layer we 'hit' with our raycast down?
private bool IsLayerPlacable(int layer) // 'int layer' is the result of gameObject.layer
 foreach (LayerMask placeableLayer in placeableLayerMasks)
        {
            Debug.Log("Comparing " + placeableLayer.value + " with " + layer);
            if (layer == placeableLayer.value) // TODO these are broken playableLayer.value is garbage??
            {
                Debug.Log("TRUEEEE");
                return true;
            }
        }

placeableLayer.value is not a valid layer value, even though I’ve configured these via the inspector (and when running, viewing this list in the inspector still shows the appropriate values).

A single LayerMask object can store several layers to match. LayerMask.value is a bit mask representing all layers. The right most bit represents layer 0. The leftmost bit represents layer 32. A bit of 1 means to match that layer, while 0 means to not match. A value of 1 ( 00000000000000000000000000000001) means layer 0 (Default) was selected. A value of 2048 ( 00000000000000000000100000000000) means layer 11 was selected. This is useful because you can specify all layers in a single LayerMask object. If layers 0 and 11 are selected the value would be layer 0 (1) + layer 11 (2048) resulting in 2049 ( 00000000000000000000100000000001). To check whether a layer should be matched use bitwise and with a left shifted 1. For example, to check whether layer 11 is matched by the layer mask do layerMask.value & 1 << 11 == 1 << 11.


private bool IsLayerPlacable(int layer) { // 'int layer' is the result of gameObject.layer
  foreach (LayerMask placeableLayer in placeableLayerMasks) {
    var bitMask = 1 << layer;
    if (placeableLayer.value & bitMask == bitMask) {
      return true;
    }
  }
}