using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponLayer : MonoBehaviour {
public string npcTag;
public LayerMask npcLayer;
public LayerMask playerLayer;
// Use this for initialization
void Start () {
if (gameObject.transform.root.gameObject.tag == npcTag)
{
gameObject.layer = npcLayer;
}
else
{
gameObject.layer = playerLayer;
}
}
}
I have this simple script to change between layers, which works fine, but I get this error. My layers exist and I just choose which one to have for npcLayer and which one for playerLayer. If my script sees that it has the npcTag it just changes to the npcLayer, anything else it chooses the playerLayer. What am I missing?
The error says that the layer index you’re trying to assign is not between 1 and 31, which is why you’re getting the error and it says what it does.
Edit: The problem is that you’re trying to assign a layer mask to a layer. You need to store ints instead of LayerMasks.
A layer identifier on a gameobject is an int with a value between 1 and 31. A layer mask is a bitwise mask for one or more layers. When you set the object’s layer to the layer mask, it implicitly converts it to a mask. So, when you choose layer 8, the mask is set to binary 00000000000000000000000010000000, which evaluates to decimal 256, far outside the allowed range.
To fix the problem, use simple ints instead of layer masks.
yeap but the thing is that I use layer default which is number 5, and my Hands layer which is in number 8...
Anyone can help me with this script? Thank you :)
– SSL7