Search Unity

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

Discussion in 'Scripting' started by MaltedWheaties, May 15, 2020.

  1. MaltedWheaties

    MaltedWheaties

    Joined:
    Apr 15, 2020
    Posts:
    44
    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
    :

    Code (CSharp):
    1. //-------------------------------------
    2. // Responsible for setting up the player.
    3. // This includes adding/removing him correctly on the network.
    4. //-------------------------------------
    5. using UnityEngine;
    6. using Mirror;
    7.  
    8. [RequireComponent(typeof(Player))]
    9. public class PlayerSetup : NetworkBehaviour
    10. {
    11.  
    12.     [SerializeField]
    13.     Behaviour[] componentsToDisable;
    14.     [SerializeField]
    15.     string remoteLayerName = "RemotePlayer";
    16.     Camera sceneCamera;
    17.     void Start()
    18.     {
    19.         // Disable components that should only be
    20.         // active on the player that we control
    21.         if (!isLocalPlayer)
    22.         {
    23.             DisableComponents();
    24.             AssignRemoteLayer();
    25.         }
    26.         else
    27.         {
    28.             // We are the local player: Disable the scene camera
    29.             sceneCamera = Camera.main;
    30.             if (sceneCamera != null)
    31.             {
    32.                 sceneCamera.gameObject.SetActive(false);
    33.             }
    34.         }
    35.     }
    36.  
    37.     public override void OnStartClient()
    38.     {
    39.         base.OnStartClient();
    40.  
    41.         string _netID = GetComponent<NetworkIdentity>().netId.ToString();
    42.         Player _player = GetComponent<Player>();
    43.  
    44.         GameManager.RegisterPlayer(_netID, _player);
    45.     }
    46.  
    47.  
    48.     void AssignRemoteLayer()
    49.     {
    50.         gameObject.layer = LayerMask.NameToLayer(remoteLayerName);
    51.     }
    52.  
    53.     void DisableComponents()
    54.     {
    55.         for (int i = 0; i < componentsToDisable.Length; i++)
    56.             {
    57.                 componentsToDisable[i].enabled = false;
    58.             }
    59.     }
    60.  
    61.     // When we are destroyed
    62.     void OnDisable()
    63.     {
    64.         // Re-enable the scene camera
    65.         if (sceneCamera != null)
    66.         {
    67.             sceneCamera.gameObject.SetActive(true);
    68.         }
    69.  
    70.         GameManager.UnRegisterPlayer(transform.name);
    71.     }
    72. }
    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.
     

    Attached Files:

  2. MaltedWheaties

    MaltedWheaties

    Joined:
    Apr 15, 2020
    Posts:
    44
  3. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    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.
     
  4. MaltedWheaties

    MaltedWheaties

    Joined:
    Apr 15, 2020
    Posts:
    44
    In the project settings "Tags & Layers" window, both LocalPlayer and RemotePlayer exist as layers, on 8 and 9.
     
  5. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
  6. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    What does this print?
    Code (CSharp):
    1. int layer = LayerMask.NameToLayer(remoteLayerName);
    2. Debug.Log($"Layer I am trying to assign is: {remoteLayerName}:{layer}");
    3. gameObject.layer = layer;
     
    sama-van likes this.
  7. sama-van

    sama-van

    Joined:
    Jun 2, 2009
    Posts:
    1,734
    Actually a bit concerned by the declaration :

    Code (CSharp):
    1.  [SerializeField]
    2.     string remoteLayerName = "RemotePlayer";
    Who know the variable could be different on request.
    Could you log it right before the assignment?
    Code (CSharp):
    1. void AssignRemoteLayer()
    2.     {
    3.         Debug.Log(remoteLayerName);
    4.         gameObject.layer = LayerMask.NameToLayer(remoteLayerName);
    5.     }
     
    Last edited: May 15, 2020
  8. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Ah yeah good point. Most likely the string has been changed in the editor or something.
     
  9. MaltedWheaties

    MaltedWheaties

    Joined:
    Apr 15, 2020
    Posts:
    44
    I get an error saying 'the name 'remoteLayerName' does not exist in the current context
     
  10. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Where did you put this code? It should go in AssignRemoteLayer()
     
  11. MaltedWheaties

    MaltedWheaties

    Joined:
    Apr 15, 2020
    Posts:
    44
    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.
    Code (CSharp):
    1.     void AssignRemoteLayer()
    2.     {
    3.         int layer = LayerMask.NameToLayer(remoteLayerName);
    4.         Debug.Log($"Layer I am trying to assign is: {remoteLayerName}:{layer}");
    5.         gameObject.layer = layer;
    6.     }
     
  12. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    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.
     
  13. Ritesh-K

    Ritesh-K

    Joined:
    May 31, 2020
    Posts:
    4
    Brackeys !