Search Unity

NetworkObject doesn't get instantiated correctly when spawning as Client

Discussion in 'Netcode for GameObjects' started by fritz-kola, Nov 13, 2021.

  1. fritz-kola

    fritz-kola

    Joined:
    Jan 4, 2016
    Posts:
    12
    So I've got a Component (ArchitectureManager), which I attached to the GameObject with the NetworkManager
    When I'm connecting as a Host, everything seems to work fine, but when I'm connecting as a Client, there are several problems.
    For debugging purposes, I build the scene and run it as a host. Then I click Play inside the Unity Editor and join as a Client.
    After joining as a client, which seems to be all fine, when I'm looking and the console output, the IsLocalPlayer-Attribute for example from the NetworkOject for the client gameObject is false and when I inspect the clients gameobject in the editor, the NetworkObject component doesn't seem to get instantiated correctly, because there aren't the information, which I can see on the hosts gameobject, like IsLocalPlayer, IsOwner etc.
    I attach two screenshots from the editor, while running the scene as a client and inspecting the gameobjects of the host and the client.

    However I can see the already joined host player and see him moving around from the clients perspective view, so this way it seems to work.
    But from the hosts perspective, I don't see the clients prefab at all, it's just not there in the scene.
    Could this be related to the problem with the clients NetworkObject-Instance?

    Here is, how I handle the spawning.

    Code (CSharp):
    1.  
    2. class ArchitectureManager : NetworkBehaviour
    3. void Start()
    4.     {
    5.         switch (SceneParameters.ArchitekturModus)
    6.         {
    7.             case "Host":
    8.                 NetworkManager.Singleton.StartHost();
    9.                 //spawn 3D Prefab
    10.                 spawnedObject = Instantiate(ThreeDimPrefab, new Vector3(32, 2.3f, -10), Quaternion.identity).gameObject;
    11.                 spawnedObject.GetComponent<NetworkObject>().SpawnAsPlayerObject(NetworkManager.Singleton.LocalClientId);
    12.                 break;
    13.             case "Client":
    14.                 NetworkManager.Singleton.OnClientConnectedCallback += OnClientConnected;
    15.                 NetworkManager.Singleton.StartClient();
    16.                 break;
    17.         }
    18.     }
    19.  
    20. void OnClientConnected(ulong clientId)
    21.     {
    22.             //spawn 3D Prefab
    23.             var tempObject = Instantiate(ThreeDimPrefab, new Vector3(34, 2.3f, -10), Quaternion.identity).gameObject;
    24.             tempObject.GetComponent<SPLNetworkObject>().SpawnPlayerServerRpc(clientId);
    25.     }
    26.  
    Code (CSharp):
    1. SPLNetworkObject : NetworkBehaviour
    2. [ServerRpc(RequireOwnership = false)]
    3.     public void SpawnPlayerServerRpc(ulong clientId)
    4.     {
    5.         //spawn 3D Prefab
    6.         this.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
    7.     }

    Can anybody enlight me? This is my first time making a multiplayer project, I'm totally stuck with this problem.
     

    Attached Files: