Not working condition: gameObject.layer==LayerMask.GetMask...
How can I check the object for several layers? The example below does not work! I want to have one variable where all the layers with which I want to compare the object can be written. This is more convenient than the NameToLayer
LayerMask layersContact;
void Start()
{
layersContact= LayerMask.GetMask("Road", "Terrain");
}
void Update()
{
if (gameObject.layer == layersContact)
{
Debug.Log("WORK!!!");
}
}
Answer by Mushroom159 · Nov 26, 2021 at 06:00 AM
@Lesnikus I had the same problem you did, and I couldn't get it to work. After reading through the unity documentation I came to the conclusion that layermask is used exclusively for physics racyast, check sphere, line cast, and the like, so it can't be used in a regular if statement. My solution was to use a check sphere, but I'm not sure if that would help you in your case. Unity Layers documentation: https://docs.unity3d.com/Manual/Layers.html Unity Layermask Documentation: https://docs.unity3d.com/ScriptReference/LayerMask.html
Your answer
![](https://koobas.hobune.stream/wayback/20220612153543im_/https://answers.unity.com/themes/thub/images/avi.jpg)