Getting the layer number from an LayerMask class

Is it possible to get a layer number from a LayerMask class? I tried LayerMask.value but it gives back the integer representing the mask. Any ideas?

example

var IgnoreThisLayer : LayerMask = 9;

function xyz (){

if (gameObject.layer == IgnoreThisLayer.value){
do something
}

}

(the code above doesn’t work)

	public static int ToLayerNumber(LayerMask mask)
	{
		for (int i = 0; i < 32; i++)
		{
			if ((1 << i) == mask.value)
				return i;
		}
		return -1;
	}

Not efficient or pretty, but it should return the layer number. Note that if there is more than one layer combined in the mask, this function returns -1.

Great! Thanks!!

@theBrandonWu A more simple way to find layer number is layerNumber = Mathf.Log(yourLayer.value,2) ,
since LayeMask number is 2^layernumber = LayerMask number

4 Likes