Set LayerMask to Everything via code c#?

I find myself needing to create LayerMasks that are set to “Everything” fairly often in code. The easiest way I know of to do that is to manually set the value in the inspector. Is there an easy way to set a LayerMask to “Everything” in code? I would need it to work regardless of how many layers are in game so it doesn’t become broken if new layers are added.

I searched and experimented but could not find a solution. I figure there might be a simple solution I haven’t found. Does anyone know of such a solution? Alternatively, does anyone know for sure that such a solution does not exist?

3 Likes
mask = ~0
29 Likes

Such a solution definitely exists, if you’re aware that a LayerMask is just a series of bits. I mean, obviously everything on your computer is, but the LayerMask is actually used as such: each bit represents a layer that is either on or off. Under the hood it’s represent as an int, and can be easily asssigned as such.

So the question is: what value of an int is all 1’s in binary? You could google that, and you’d get a number which you could plug in (you want the result of 2^32 - 1) and it’d work. But we can do this in a slightly cleaner way, using a bitwise NOT operator, which in C# is tilde (~) - which will literally reverse every bit in a value. So now the question is, what int value is all 0’s in binary?

Spoiler: it’s 0.

LayerMask someMask = ~0;

edit: beaten to it by someone who just had to type the answer. :wink: Well, now you know how that answer was arrived at.

42 Likes

Thanks mikael_juhala and StarManta. That works great.

1 Like

LayerMask everything = -1;

This works thanks to twos-complement representation.

4 Likes

What about setting the layer mask to “Nothing”?

Each bit is a layer obviously so that would mean no bits set (zero).

1 Like

For anyone wandering into here, bit operations are a powerful and very fast way to handle masks.
You can have

int layer1 = 1;       // ..0001 as bits, 1 as a number
int layer2 = 1 << 2;  // ..0010 as bits, 2 as a number
int layer3 = 1 << 3;  // ..0100 as bits, 4 as a number
int layer4 = 1 << 4;  // ..1000 as buts, 8 as a number

Then you compare them with AND, OR and other operators.

int res1 = layer1 & layer2  // ..0011 as bits, 3 as a number (2+1)
int res2 = layer1 & layer3  // ..0101 as bits, 5 as a number (4+1)

if (1 << gameObject.layer & layer1) > 0)
{
  // do stuff
}

The above if-statement compares two layers to each other, and if they are the same (have the on-bit in the same place) it returns true because the resulting int has all bits on that are on in both. The fact that gameObject.layer is an int representing the number of the layer in the layer list complicates it a little, but not much. To get that as a bit, just do the 1 << layer operation to get it as bit representation.

Layer Masks in Depth – Mathias Soeholm – Game Developer and CS student in Aarhus ← nice explanation of how bit flagging works.

You could also build preset masks with the OR operator:

public static class Layers
{
    public const int Character = 1 << 6;
    public const int Destructible = 1 << 7;
    public const int Indestructible = 1 << 8;
    public const int ProjectileHits = Character | Destructible | Indestructible;
}
5 Likes

Also, Physics.AllLayers exists.

Yes, specifically these:

https://docs.unity3d.com/ScriptReference/Physics.AllLayers.html
https://docs.unity3d.com/ScriptReference/Physics2D.AllLayers.html

2 Likes

Its a problem because LayerMask is an int, which is signed meaning you can’t normally just set bits without using negative values because the language syntax processor is checking to make sure you don’t make an error.

But, if you know what you are doing, you can just use an unchecked syntax that and it will let you set the bits, like so:

[SerializeField] LayerMask m_myLayerMask = unchecked((int)0xffffffff);

If you really know what you’re doing you can set it to -1 or even ~0

“There are 10 types of people in this world: those who understand binary, and those who don’t.”

The difference between Layers vs LayerMasks:

https://forum.unity.com/threads/raycast-layermask-parameter.944194/#post-6161542

3 Likes

That’s what Bitwise/Shift operators on int, uint, long and ulong are for. There should be no need to use unchecked in this case.

https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/bitwise-and-shift-operators

As Kurt points out with an example being here: https://github.com/Unity-Technologies/UnityCsReference/blob/c0058685e1f312abb957997394e413664948b00d/Modules/Physics/ScriptBindings/Physics.bindings.cs#L89

Honestly, only thing I wanted was LayerMask.All or something. Seriosuly, who looks up manual of Physics namespace when there’s already LayerMask class and GetMask or something are there? Especially, in the stuation where online manual constantly gives me " Sorry… that page seems to be missing!", duh!

3 Likes