Ignoring Layer Condition + LayerMask = problem

Hello.

Regarding the script below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Game.Controllers {
   [System.Serializable]
   public struct IgnoreLayersStruct {
      public LayerMask Layer1;
      public LayerMask Layer2;
   }

   public class GameController : MonoBehaviour {
      public IgnoreLayersStruct[] ignoreLayers;

      // Use this for initialization
      private void Awake () {
         DoIgnoreLayers();
      }

      private void DoIgnoreLayers() {
         foreach (IgnoreLayersStruct L in ignoreLayers) {
            Physics.IgnoreLayerCollision(L.Layer1.value, L.Layer2.value);
         }
      }
   }
}

I am receiving the error:

By debugging it, Layer1 have value of 256 and Layer2 have a value of 512.
May i have some help on how to solve this problem?

Devs: This is a bug! shouln’d work like this! LayerMask should give us the right value of the mask since it’s expecting a value between 0 and 31.

It’s not really a bug, the layer mask is a bitmask so it’s asking you which layer numbers you want.

For example, layer number 8 would be 256 (1 << 8) and layer number 9 would be 512 (1 << 9 ).

A return value of 768 would mean that layers 8 and 9 are both set.

hmm… So, i just have to bitwise it to get the value. Would be nice to LayerMask have a method that return this value for this purpose. I still don’t see the use of the brute value that LayerMassk.Value returns if i have to transform it.

thank you so much for the help!

Yeah, it’s normally returned like that so you can check which layers are selected.

For instance, you might do:

if( ReturnedLayerMask & ( 1 << LayerMask.NameToLayer( "Buildings" ) )
{

}

I had forget that in projects Physics i can select which layer collides with what.
And besides, string layer name is really a pain when we already have LayerMask and knowing that we cannot even use it’s value to work with Physics without having to make calculations. Really disappointing.

Anyway. I get it and fixed. thank you!