BoxCollider2d include/exclude layers selects wrong layers?

I’ve added a layer called “Black” and I am now executing the following testcode:

gameObject.layer = LayerMask.NameToLayer("Black");
GetComponent<BoxCollider2D>().excludeLayers = LayerMask.NameToLayer("Black");

The layer of the gameobject is successfully changed to the new layer. However, the .excludeLayers gets set to ‘Default, Ignore Raycast, Water’. In my eyes this should be set to ‘Black’, right?

For another test I’ve added the layer “Red”, change the code accordingly from Black to Red. Now .excludeLayers is set to ‘Ignore Raycast, Water’.

I wonder what I’m doing wrong here. :slight_smile:

Okay, so, I used my partner as Rubber Duck and I found the issue.

  • gameObject.layer expects the layer index which is retrieved by ‘LayerMask.NameToLayer’
  • .excludeLayers expects a or multiple layermasks, which I learned, is not the same as the layer index.

For future reference, the code should be

gameObject.layer = LayerMask.NameToLayer("Black");
GetComponent<BoxCollider2D>().includeLayers = LayerMask.GetMask("Black");
1 Like

Thanks for taking the time to come back and post your solution!!

For anyone else wondering why the above is the fix:

The difference between Layers vs LayerMasks:

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

1 Like

Also the fact that one is plural (“layers”) and one is not (“NameToLayer”) should also be a red flag to you.

Masks are used all over the place in Unity; this isn’t exclusive to this property.

TBH, your very first action should be to look at the docs which would show this immediately: Unity - Scripting API: Collider2D.excludeLayers

MelvMay, I find your reply quite unprofessional and unhelpful.

  • I did notice one is plural and the other is not. But the solution ‘GetMask’ isn’t plural either.
  • TBH, You assume I didn’t read the docs, I however did. But maybe, just maybe, it didn’t help me and that’s why I asked a question. And afaik, that’s what these forums are for.

It’s unfortunate you find suggesting that you read the docs unprofessional and unhelpful being as they do state that the property requires a “LayerMask” and that NameToLayer doesn’t return that type. It’s also unfortunate you didn’t understand the docs but it’s not unprofessional to suggest you read them and to highlight why they’d give you the answer if you looked closer.

A mask is singular because it is a single thing, a mask which contains the specification of layers. A bit-mask is a fundamental thing in programming where each bit is used as a flag.

The scripting docs assume a basic working knowledge of such things for sure.

Because unfortunately that’s extremely prevelant on these forums. It seems you did read them but didn’t understand them.

2 Likes

Look, you’re right. And yes, people should read the docs, and I did. The thing I found was “unprofessional” is that your comment felt degrading to me. Maybe it’s how I read it, things can “sound” different online as you know since there is no context further then the actual text.

Let bygones be bygones, no hard feelings.

2 Likes

No hard feelings at all! Pushing devs toward the docs is a daily occurence and not personal. :slight_smile: