Question about layermask issue,Layer Mask not working but the int value does?

In my code I have a layer mask defined

public LayerMask death_layer;

but when I do this it doesn’t work

private void OnTriggerEnter2D(Collider2D collision)
        {
            if(collision.gameObject.layer == death_layer)
            { 
                Debug.Log("aaa");
            }         
        }

but if I change it to the int of the layer 6 it works

private void OnTriggerEnter2D(Collider2D collision)
        {
            if(collision.gameObject.layer == 6)
            { 
                Debug.Log("aaa");
            }         
        }

I know i could keep it as 6 but I just want an answer about how this issue is happening.

Hi,

The reason for this is somewhat confusing usage of layer variable by Unity. First layer variable on gameObject is an int (and its index of layer in the layer list), but death_layer variable is a LayerMask so it’s actually bit mask and can potentially have multiple values (eg. you can set LayerMask to layers UI and Water). Even in your example if you select only sixth layer created by you it DOES NOT return 6, but 64 (because 2 to a power of 6 is exactly 64). So in short gameObject.layer returns index of a layer, but LayerMask returns bit value.