Possible bug with LayerMask.NameToLayer but wanted to check first...

I’ve used LayerMasks a lot in the past but right now I’m having a really weird interaction and wanted to make sure it wasn’t something stupid I was doing before submitting it as a bug. I’ve got layers called “Player” and “Enemy” as well as an “Allegiance” setting that is either player or enemy as well. Based on your allegiance, I’m changing the layerMask of your attack to hit your opponent. Here’s my code:

public Allegiance allegiance;
public LayerMask enemyLayers;

public Start() {
  enemyLayers = allegiance == Allegiance.Player ? LayerMask.NameToLayer("Enemy") : LayerMask.NameToLayer("Player");
}

When this runs and I check the script in the inspector, the one set to “Player” does not have the player layer selected but instead has the Default, TransparentFX, and Ignore Raycast layers selected. The enemy has only has TransparentFX and Ignore Raycast. They both should have none of that as the default setting is “Nothing”. This happens even if I just set enemyLayers to be Enemy or Player without the conditional.

Any idea why this is happening?

NameToLayer returns an integer representing the index of the layer - NOT a layermask
you can either construct a layermask manaully, or perhaps you should be using

1 Like

Ahh that did it, thank you! I had been using NameToLayer to set the object’s layer and assumed it would work the same way for the mask but I can see now they’re pretty different.