- Home /
A game object can only be in one layer. The layer needs to be in the range [0...31]
I am following a series of tutorials as closely as I can, but I am having a problem. I think this may be the source. When I have 2 players in the scene running in different instances of the game, one is supposed to be local, and the other is supposed to be remote. however, both stay local, and it gives me the error "A game object can only be in one layer. The layer needs to be in the range [0...31]" Here is my full script:
using UnityEngine; using UnityEngine.Networking;
public class PlayerSetup : NetworkBehaviour {
[SerializeField]
Behaviour[] componentsToDisable;
[SerializeField]
string remoteLayerName = "RemotePlayer";
Camera sceneCamera;
void Start()
{
if (!isLocalPlayer)
{
DisableComponents();
AssignRemoteLayer();
}
else
{
sceneCamera = Camera.main;
if (sceneCamera != null)
Camera.main.gameObject.SetActive(false);
}
RegisterPlayer();
}
void RegisterPlayer()
{
string _ID = "Player " + GetComponent<NetworkIdentity>().netId;
transform.name = _ID;
}
void AssignRemoteLayer()
{
gameObject.layer = LayerMask.NameToLayer(remoteLayerName);
}
void DisableComponents()
{
for (int i = 0; i < componentsToDisable.Length; i++)
{
componentsToDisable[i].enabled = false;
}
}
void OnDisable()
{
if (sceneCamera != null)
sceneCamera.gameObject.SetActive(true);
}
}
Answer by Farwall · Aug 30, 2017 at 09:43 AM
I know its bit a late reply. But might be helpfull for others. So This error comes when Layer is missing. Goto Edit > Project Settings > Tags and Layers You need to add Layers Name which you are using.
This answer was very helpful to me. :) (unlike the error in Unity) I couldn't figure out what was going on. Was I somehow simultaneously setting my object to two layers via some crazy unseen multithreading going on?
Nope. Didn't set up my layer. :P
Answer by AhmedSabry19 · Mar 20, 2016 at 02:01 PM
@overseer314 I had the same problem with brackies tutorial series :D I solved it by adding this is the Start()
mask = LayerMask.NameToLayer("RemotePlayer") ;
Answer by Eyliess · Jun 16, 2019 at 01:55 PM
Old post, but this also happens if you try to assign a GameObject's layer to a Layer Mask, a silly mistake but it won't come up as an error in the compiler.
Of course. Any time you try to assign a layer index that is smaller than 0 or greater than 31 this error will be thrown. In the case of the OP he used LayerMask.NameToLayer which returns "-1" in case the layer name doesn't exist. Assigning a layermaks does not automatically mean you get that error, only when the actual value of your mask is greater than 31. This happens when the mask contains any layer above layer 5. Any yes, assigning a layermask to the layer index property won't work. From the compiler perspective a layer index as well as a layer mask are both just integer values. So there's no way the compiler could detect this mistake.
Thanks for the clarification. I just wanted to mention it despite of what the OP's issue is because when googling the error it's the first thing that comes up.
Answer by wajidali892 · Apr 16, 2018 at 11:49 AM
The other way of doing this is simple and easy By declaring a enum playerLayers {LocalPlayer=8,RemotePlayer=9} Then assigning the gameobject.layer the value as follows Gameobject.layer = (int)playerLayers.RemotePlayer The remote player value is used in your case. I hope you guys like this solution
Answer by MoMughal · Aug 27, 2018 at 12:38 AM
I just started watching Brackeys and got the same problem, got it fixed simply by hard coding the layer name in the function
void AssignRemoteLayer() { gameObject.layer = LayerMask.NameToLayer("RemotePlayer"); }
I guess the variable is not being set on start thus the error
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Detecting that I'm clicking a unit even though I'm not? 0 Answers
Trouble Setting/Combining LayerMask with "Builtin" Layer 0 Answers