A game object can only be in one layer. The layer needs to be in the range [0...31] (791004)

Hi there, I am making a multiplayer FPS with Mirror.
I am following an old Brackeys tutorial, changing the obselete HLAP to Mirror.

Here is my player prefab window:


https://imgur.com/a/Wi6iyiI

On both the client and the host side, I keep getting this message:

(Filename: C:/Users/Useraname/OneDrive/Documents/MultiplayerFPS/Assets/Mirror/Runtime/Transport/Telepathy/Server.cs Line: 82)

A game object can only be in one layer. The layer needs to be in the range [0…31]
UnityEngine.GameObject:set_layer(Int32)
PlayerSetup:AssignRemoteLayer() (at C:\Users*Username*\OneDrive\Documents\MultiplayerFPS\Assets\Scripts\PlayerSetup.cs:50)
PlayerSetup:Start() (at C:\Users*Username*\OneDrive\Documents\MultiplayerFPS\Assets\Scripts\PlayerSetup.cs:24)

This is PlayerSetup.cs:

//-------------------------------------
// Responsible for setting up the player.
// This includes adding/removing him correctly on the network.
//-------------------------------------
using UnityEngine;
using Mirror;

[RequireComponent(typeof(Player))]
public class PlayerSetup : NetworkBehaviour
{

    [SerializeField]
    Behaviour[] componentsToDisable;
    [SerializeField]
    string remoteLayerName = "RemotePlayer";
    Camera sceneCamera;
    void Start()
    {
        // Disable components that should only be
        // active on the player that we control
        if (!isLocalPlayer)
        {
            DisableComponents();
            AssignRemoteLayer();
        }
        else
        {
            // We are the local player: Disable the scene camera
            sceneCamera = Camera.main;
            if (sceneCamera != null)
            {
                sceneCamera.gameObject.SetActive(false);
            }
        }
    }

    public override void OnStartClient()
    {
        base.OnStartClient();

        string _netID = GetComponent<NetworkIdentity>().netId.ToString();
        Player _player = GetComponent<Player>();

        GameManager.RegisterPlayer(_netID, _player);
    }


    void AssignRemoteLayer()
    {
        gameObject.layer = LayerMask.NameToLayer(remoteLayerName);
    }
 
    void DisableComponents()
    {
        for (int i = 0; i < componentsToDisable.Length; i++)
            {
                componentsToDisable[i].enabled = false;
            }
    }

    // When we are destroyed
    void OnDisable()
    {
        // Re-enable the scene camera
        if (sceneCamera != null)
        {
            sceneCamera.gameObject.SetActive(true);
        }

        GameManager.UnRegisterPlayer(transform.name);
    }
}

My other scripts are attached below:

There is also a short PlayerWeapon.cs script that contains weapon information, however I feel this is irrelevant.

I also can not move on the unity editor. I imagine this is related to this issue, and will be resolved when this issue is.

5852992–621760–GameManager.cs (1.06 KB)
5852992–621763–Player.cs (692 Bytes)
5852992–621766–PlayerController.cs (2.16 KB)
5852992–621769–PlayerMotor.cs (1.87 KB)
5852992–621778–PlayerShoot.cs (959 Bytes)

The working imgur link:

Does the “RemotePlayer” layer exists in both project’s settings?
This error often occur while calling a layer from name an unfortunately doesn’t exist in the Project Settings.

In the project settings “Tags & Layers” window, both LocalPlayer and RemotePlayer exist as layers, on 8 and 9.

Exact same code, exact same error from a 5 years old topic o_o…

What does this print?

int layer = LayerMask.NameToLayer(remoteLayerName);
Debug.Log($"Layer I am trying to assign is: {remoteLayerName}:{layer}");
gameObject.layer = layer;
1 Like

Actually a bit concerned by the declaration :

 [SerializeField]
    string remoteLayerName = "RemotePlayer";

Who know the variable could be different on request.
Could you log it right before the assignment?

void AssignRemoteLayer()
    {
        Debug.Log(remoteLayerName);
        gameObject.layer = LayerMask.NameToLayer(remoteLayerName);
    }

Ah yeah good point. Most likely the string has been changed in the editor or something.

I get an error saying 'the name ‘remoteLayerName’ does not exist in the current context

Where did you put this code? It should go in AssignRemoteLayer()

I put the three lines in AssignRemoteLayer() and it gives me the same error.

As far as I can tell nothing is printed to the console.

    void AssignRemoteLayer()
    {
        int layer = LayerMask.NameToLayer(remoteLayerName);
        Debug.Log($"Layer I am trying to assign is: {remoteLayerName}:{layer}");
        gameObject.layer = layer;
    }

Your code before was already using the variable “remoteLayerName” in that spot, so you must have changed something else in the interim to get that error now.

Brackeys !