Search Unity

Question Can't spawn player object on client

Discussion in 'Netcode for GameObjects' started by giratina5055, Dec 11, 2021.

  1. giratina5055

    giratina5055

    Joined:
    Aug 3, 2020
    Posts:
    3
    Whenever i go to spawn a player object using networkObject.SpawnAsPlayerObject(clientId), the player spawns in correctly on the host but i get a KeyNotFoundException on the client.
    Code (CSharp):
    1. [ServerRpc(RequireOwnership = false)]
    2.     public void SpawnPlayerServerRpc(ulong clientId)
    3.     {
    4.         // Get Spawn.  Stop if there are no spawn points in the seen
    5.         Debug.Log("Spawinging player");
    6.         Transform spawn = GetSpawnPoint();
    7.         if (spawn == null) { Debug.Log("No Spawn Points in Scene!"); return; }
    8.         Debug.Log(spawn.position + " is spawn");
    9.         // Spawn on Client
    10.         GameObject go = Instantiate(_playerPrefab, spawn.position, spawn.rotation);
    11.  
    12.  
    13.         Debug.Log("clientId = " + clientId);
    14.  
    15.         go.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
    16.         //go.GetComponent<NetworkObject>().SpawnWithOwnership(clientId);
    17.         ulong objectId = go.GetComponent<NetworkObject>().NetworkObjectId;
    18.  
    19.         SpawnClientRpc(objectId);
    20.  
    21.     }
    22.  
    23.     // A ClientRpc can be invoked by the server to be executed on a client
    24.     [ClientRpc]
    25.     private void SpawnClientRpc(ulong objectId)
    26.     {
    27.         //NetworkObject player = NetworkSpawnManager.SpawnedObjects[objectId];
    28.         NetworkObject player = NetworkManager.Singleton.SpawnManager.SpawnedObjects[objectId];
    29.     }
    Any help would be appreciated :)).
     
    barisdincer likes this.
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    660
    It's probably related to the clientId, check it's correct for the client.
     
  3. abhram13

    abhram13

    Joined:
    Jun 16, 2021
    Posts:
    1
    hi giratina5055 i am using the same code but having another error.basicly the error is(
    Don't know how to serialize Transform - implement INetworkSerializable or add an extension method for FastBufferWriter.WriteValueSafe to define serialization.) and it happens on the spawn position and rotation values. can you please share all the code
     
  4. Grave188

    Grave188

    Joined:
    Dec 13, 2017
    Posts:
    10
    Use this doc. Short, implement INetworkSerializable interface to your custom struct
    https://docs-multiplayer.unity3d.co...ced-topics/serialization/inetworkserializable