Getcontacts() layermask not working

Hey, I’m trying to use a layermask to filter results with getcontacts but it’s not working.

//reads all overlapping labels, saves them in the array and returns the amount in the array
Collider2D[] contactList = new Collider2D[5];
ContactFilter2D overlapFilter;

        public int GetOverlapContacts() {
            //int overlapAmount = GetComponent<Rigidbody2D>().OverlapCollider(overlapFilter,contactList);
            //overlapCount = GetComponent<BoxCollider2D>().OverlapCollider(overlapFilter, contactList);
            overlapFilter.useTriggers = true;
            overlapFilter.useLayerMask = true;
            overlapFilter.layerMask = LayerMask.NameToLayer("OverlapLabel");
            //overlapFilter.NoFilter();

            overlapCount = GetComponent<Rigidbody2D>().GetContacts(overlapFilter, contactList);
            Debug.Log("overlapCount " + overlapCount + " overlapFilter "+ overlapFilter.isFiltering);
            return overlapCount;
        }

The debug just returns 0, both the rigidbodies are overlapping and they are both on the overlapLabel layer.
Something I’m missing?

Ok so the solution is that you still need to bitshift the layermask.
So use

overlapFilter.layerMask = LayerMask.GetMask("OverlapLabel");
//or
overlapFilter.layerMask = 1 << LayerMask.NameToLayer("OverlapLabel");

Thanks for the help!

Bitshifting worked.

Just an additional question, why does bitshifting that work?

You revived a dead thread from 6 years ago.

A Layer Number (also called a Layer Index) is an integer in the range 0 to 31. (In the Inspector, they only show 0 to 30.) The LayerMask.NameToLayer() function returns a Layer Number, not a LayerMask value.

A LayerMask itself is a packed set of 32 bits, each bit representing one of the 32 possible layers. This lets you combine any possible combination of all 32 layers in a single value.

To convert from a Layer Number to a LayerMask value, you need to do some bit shifting.

Some quick shortcuts converting Layer Numbers to LayerMask Values and other operations.

LayerMask mask = (1 << layerNumber);

LayerMask mask = (1 << layerNumberA) | (1 << layerNumberB) | (1 << layerNumberC);

LayerMask mask = (1 << layerNumber) | ((int)(layerMaskToInclude));

LayerMask mask = ((int)(layerMaskToInclude)) & ~((int)(layerMaskToExclude));

LayerMask mask = 0; // no layers

LayerMask mask = ~0; // all layers

bool isObjectOnALayerInLayerMask = ((1 << gameObject.layer) & layerMaskToInclude) != 0;