Search Unity

Resolved Unexpected Behavior of OwnerClientId in Player Prefab

Discussion in 'Netcode for GameObjects' started by AmadeusSalieri, Jan 24, 2024.

  1. AmadeusSalieri


    Jan 4, 2021
    Hello Unity Developers,

    I am encountering a confusing situation with the OwnerClientId behavior in a networked game using Unity. I have set up a Spawner class, which is configured as the Network Manager's Player Prefab(The Spawner class, more specifically, the Spawner game object prefab that includes both the Spawner component and the Network Object component, is configured as the Player Prefab in the Network Manager). My primary concern revolves around the behavior of OwnerClientId in different network states (i.e., with and without clients connected).

    Here's the relevant snippet of my Spawner class:

    Code (CSharp):
    1. public class Spawner : NetworkBehaviour {
    2.     // ... (code omitted for brevity)
    3.     public void Spawn() {
    4.         if (IsHost) {
    5.             // These two lines of code produce different results.
    6.             // And the line below produces the result I intended.
    7.             SpawnLogic(OwnerClientId);
    8.             SpawnLogic(NetworkManager.Singleton.LocalClientId);
    9.             // ... (rest of the method)
    10.         }
    11.         else if (IsClient && IsLocalPlayer)
    12.         {
    13.             SpawnServerRpc();
    14.             // ... (rest of the method)
    15.         }
    16.         // ... (other conditions)
    17.     }
    19.     [ServerRpc]
    20.     private void SpawnServerRpc(ServerRpcParams serverRpcParams = default) {
    21.         SpawnLogic(serverRpcParams.Receive.SenderClientId);
    22.         // ... (rest of the method)
    23.     }
    25.     // ... (rest of the class)
    26. }

    Scenario and Issue:

    • When the game is running with both a host and a client connected, and the host calls the Spawn() function, OwnerClientId unexpectedly represents the client's ID instead of the host's own ID.
    • However, if the host is running alone without any clients, OwnerClientId correctly reflects the host's own ID.
    • Modifying the code to SpawnLogic(NetworkManager.Singleton.LocalClientId) works as intended, where the host spawns players with its own ID, and clients request the server (host) to spawn players with their respective IDs.
    • My understanding is that the Network Manager should automatically assign a Spawner to each player (host and client) with corresponding ownership. Hence, OwnerClientId in the Spawner object should always be the ID of the owning player.


    • Why does the OwnerClientId differ from NetworkManager.Singleton.LocalClientId in the scenario where the client is connected?
    • Is there a conceptual misunderstanding on my part, or is this behavior unexpected?

    I appreciate any insights or explanations that can help clarify this behavior. Thank you for your assistance!
    Last edited: Jan 24, 2024
  2. AmadeusSalieri


    Jan 4, 2021
    It was my mistake. I was using a debugging tool to directly call the Spawn() function, and I realized that I was calling the Spawn() function of the last added Spawner among several (both host and clients). Once I made sure to call the Spawn function from the Spawner that I actually own, the issue was resolved. It was entirely my error.
  3. NoelStephens_Unity


    Unity Technologies

    Feb 12, 2022
    Thank you for letting us know! :)