Layer mask return true when should be false

Im currently having a problem that my layer mask is returning true to a layer that should be set to false. Im running a check on all objects within the bounds of an object and getting all renders and then checking them against a layer mask to see if they are valid. Whats happening is that the layer mask on that object is returning true for a layer that it should be returning false to. The layer its checking against is called “Weapon” and on the affectedLayers variable this variable is layer in the mask is set to false. However when running through the code it is returning true for this layer within the mask.

    private static GameObject[] GetAffectedObjects(Bounds bounds, LayerMask affectedLayers) {
        MeshRenderer[] renderers = (MeshRenderer[]) GameObject.FindObjectsOfType<MeshRenderer>();
        List<GameObject> objects = new List<GameObject>();
        foreach(Renderer r in renderers) {
            if( !r.enabled ) continue;
            if( !IsLayerContains(affectedLayers, r.gameObject) ) continue;
            if( r.GetComponent<Decal>() != null ) continue;
           
            if( bounds.Intersects(r.bounds) ) {
                objects.Add(r.gameObject);
            }
        }
    private static bool IsLayerContains(LayerMask mask,GameObject obj)
    {
        if(mask == (mask | 1 << obj.layer))
        {
            Debug.Log(true + obj.name);
        }
        else
        {
            Debug.Log(false + obj.name);
        }

        return mask == (mask | 1 << obj.layer);
    }

I then have another layer that is returning false when its bool is set to true (3 layers above the weapon layer). Although this is not important as that layer is not associated in any way with the area of code its interesting that when placing a cube tagged with that layer in the bounds it returns false for it when its still set to true in the layer mask.

Anyone got any ideas what is causing this to happen

The IsLayerContains code works fine on my end. That means that there’s probably an error somewhere else.

You might be sending in bad data as the Mask. If you’re sending in the result of LayerMask.NameToLayer(“Weapon”) rather than 1 << LayerMask.NameToLayer(“Weapon”), that could be the issue. If you’re assigning it in the inspector, that won’t be it.

Another issue could be if you’re looking at the wrong object - some object could have the same name as the one you’re looking at. Do your debugs like this:

Debug.Log(true + obj.name, obj);

That way, if you click the debug message, the object will flash in the hierarchy.

Also make sure that you’re not running into an issue where a child object has a different layer than the parent object, and you’re looking at the wrong thing.

Thanks for the response

The mask is assigned in the inspector and then that variable is being passed in as the mask field, so unless something wierd is going on with the inspector id imagine its not that.
I updated the debug to highlight the object begin checked and this did also return the correct object. I have also checked all the child/parent objects and all of them are correct.
I have found a work around for the time being which was to check the tag and break out if its set to “weapon” and that has fixed it for the time being, not an ideal solution though