Rigidbody2D object.OverlapCollider(ContactFilter2D, Collider2D[]) not working when used with LayerMask

I am attempting to find if two 2D objects are overlapping. I have a main object (mo) and an interactable object (io). mo has a Rigidbody2D and a CapsulCollider2D. io has a CircleCollider2D. And a bit of code that looks similar to this:

public class CharacterMover : MonoBehaviour {
	private ContactFilter2D interactFilter;
	private Rigidbody2D rigidBody;

	void Start () {
		rigidBody = GetComponent<Rigidbody2D>();
		interactFilter = new ContactFilter2D();
		LayerMask mask = LayerMask.NameToLayer("Interactable");

	void Update() {
		//Some other stuff happens like moving and whatever.

		Collider2D[] results = new Collider2D[1];
		int resultCount = rigidBody.OverlapCollider (interactFilter, results);
		if (resultCount > 0) {
			//Do some stuff

However, when mo and io are in the same spot nothing happens. However, if I adjust my interactFilter to not have a layer mask, among the various things that get returned (like the floor and background), io does show up.

I have checked and am certain that there isn’t a typo in my LayerMask name- it returns layer 12 which matches up with the layer in my editor. Obviously, I can handle all the filtering myself, but I’d really rather not- specifically because that means that my result array needs to be larger than 1 which is unwanted in this specific situation.

I’m assuming there’s something boneheaded going on on my part. Does anyone know what it could be?

The problem lies with LayerMask.NameToLayer. It’s a bit counter-intuitive, but: NameToLayer returns the index of the Layer with the given name. So you get 1, 2, 3, 4 and so on when you go through your layers’ names.
A LayerMask, however, is a single integer that represents a set of layers as a bitmask.

In binary, your returned layer index is “…001000” (binary for 8).

A LayerMask uses the binary number’s bits as a collection booleans. So if you had layers 1 and 3 in a mask, the LayerMask would look like this: “…00101”, which, seen as a number rather than a bitmask, would be 5.

So what you would want to have is not “…001000” but the 8th bit from the left being 1, “…0010000000” which would be interpreted as 128.

Long story short: Don’t use NameToLayer for generating a LayerMask, use either GetMask


or expose the LayerMask to the editor

public LayerMask layers;
// further down in the method