Search Unity

Problem with Layer

Discussion in 'Multiplayer' started by r9wx, Sep 24, 2022.

  1. r9wx

    r9wx

    Joined:
    Sep 24, 2022
    Posts:
    4
    I follow a tutorial on youtube to create your multiplayer games and then for some time there is this error that appears I do not know where the problem comes from.

    Link video:


    the error in question:

    A game object can only be in one layer. The layer needs to be in the range [0...31]
    UnityEngine.StackTraceUtility:ExtractStackTrace ()
    Util:SetLayerRecursively (UnityEngine.GameObject,int) (at Assets/Scripts/Util.cs:7)
    Util:SetLayerRecursively (UnityEngine.GameObject,int) (at Assets/Scripts/Util.cs:11)
    Util:SetLayerRecursively (UnityEngine.GameObject,int) (at Assets/Scripts/Util.cs:11)
    Util:SetLayerRecursively (UnityEngine.GameObject,int) (at Assets/Scripts/Util.cs:11)
    WeaponManager:EquipWeapon (PlayerWeapon) (at Assets/Scripts/WeaponManager.cs:49)
    WeaponManager:Start () (at Assets/Scripts/WeaponManager.cs:20)


    PlayerSetup:

    Code (CSharp):
    1. using UnityEngine;
    2. using Mirror;
    3.  
    4. [RequireComponent(typeof(Player))]
    5. [RequireComponent(typeof(PlayerController))]
    6. public class PlayerSetup : NetworkBehaviour
    7. {
    8.     [SerializeField]
    9.     Behaviour[] composentsToDisable;
    10.  
    11.     [SerializeField]
    12.     private string remoteLayerName = "RemotePlayer";
    13.  
    14.     [SerializeField]
    15.     private string dontDrawnLayerName = "DontDraw";
    16.  
    17.     [SerializeField]
    18.     private GameObject playerGraphics;
    19.  
    20.     [SerializeField]
    21.     private GameObject playerUIPrefab;
    22.  
    23.     [HideInInspector]
    24.     public GameObject playerUIInstance;
    25.  
    26.  
    27.     private void Start()
    28.     {
    29.         if(!isLocalPlayer)
    30.         {
    31.             DisableComponents();
    32.             AssignRemoteLayer();
    33.         }
    34.         else
    35.         {
    36.  
    37.  
    38.             Util.SetLayerRecursively(playerGraphics, LayerMask.NameToLayer(dontDrawnLayerName));
    39.  
    40.             playerUIInstance = Instantiate(playerUIPrefab);
    41.  
    42.             PlayerUI ui = playerUIInstance.GetComponent<PlayerUI>();
    43.             if(ui == null)
    44.             {
    45.                 Debug.LogError("Pas de component Player UI sur playerUIIinstance");
    46.             }
    47.             else
    48.             {
    49.                 ui.SetController(GetComponent<PlayerController>());
    50.             }
    51.  
    52.             GetComponent<Player>().Setup();
    53.         }
    54.  
    55.  
    56.     }
    57.  
    58.     public override void OnStartClient()
    59.     {
    60.         base.OnStartClient();
    61.  
    62.         string netId = GetComponent<NetworkIdentity>().netId.ToString();
    63.         Player player = GetComponent<Player>();
    64.  
    65.         GameManager.RegisterPlayer(netId, player);
    66.     }
    67.  
    68.     private void AssignRemoteLayer()
    69.     {
    70.         gameObject.layer = LayerMask.NameToLayer(remoteLayerName);
    71.     }
    72.  
    73.     private void DisableComponents()
    74.     {
    75.         for (int i = 0; i < composentsToDisable.Length; i++)
    76.         {
    77.             composentsToDisable[i].enabled =false;
    78.         }
    79.     }
    80.  
    81.     private void OnDisable()
    82.     {
    83.         Destroy(playerUIInstance);
    84.         if(isLocalPlayer)
    85.         {
    86.             GameManager.instance.SetSceneCameraActive(true);
    87.         }
    88.  
    89.         GameManager.UnregisterPlayer(transform.name);
    90.     }
    91. }
    92.  
     

    Attached Files:

    Last edited: Sep 24, 2022
  2. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    It would be useful to include the error, we can't see it from here :)
     
  3. r9wx

    r9wx

    Joined:
    Sep 24, 2022
    Posts:
    4
    a yes sorry my bad
     
  4. mischa2k

    mischa2k

    Joined:
    Sep 4, 2015
    Posts:
    4,347
    Looks like a Unity error, not netcode related.
    Check out the Unity manual on layers :)
     
  5. r9wx

    r9wx

    Joined:
    Sep 24, 2022
    Posts:
    4
    ok thanks
     
  6. r9wx

    r9wx

    Joined:
    Sep 24, 2022
    Posts:
    4
    ok i searched but i didn't find anything good can you help me it's my first time with unity