Home > Back-end >  LayerMask.NameToLayer not returning the expected value
LayerMask.NameToLayer not returning the expected value

Time:02-22

I have the below code:

LayerMask targetLayer;
targetLayer = LayerMask.NameToLayer("TeamB");

However, instead of selecting my named layer, it selects 3 random ones (seemingly)? Default, TransparentFX and Ignore Raycast.

I also tried:

targetLayer = 7;

As I know that is the index of the layer, but it still doesn't work. What am I doing wrong? How do I programatically set my named layer?

CodePudding user response:

The LayerMask type is actually a struct and you are trying to assign it an integer value.

To get a LayerMask return type, you should use:

targetLayer = LayerMask.GetMask("TeamB");

The documentation explains the differences clearly:

  • NameToLayer(string layerName) - Given a layer name, returns the layer index as defined by either a Builtin or a User Layer in the Tags and Layers manager.
  • GetMask(params string[] layerNames) - Given a set of layer names as defined by either a Builtin or a User Layer in the Tags and Layers manager, returns the equivalent layer mask for all of them. You can pass in multiple names in a string[].
  • Related