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