Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Client player object won't be created

Discussion in 'Netcode for GameObjects' started by Michel3033, Apr 4, 2023.

  1. Michel3033

    Michel3033

    Joined:
    Jan 1, 2013
    Posts:
    2
    Hello,

    I am trying to run a simple multiplayer scene of a hodt and a client walking around. As I have many other scripts accessing the player prefab it's best for me to instantiate those on my own. I am using the following code for this, mostly taken from another post here:
    Code (CSharp):
    1. [ServerRpc(RequireOwnership = false)]
    2. public void SpawnPlayerServerRpc(ulong clientId)
    3. {
    4.     Debug.Log("Spawning player with id: " + clientId);
    5.     GameObject newPlayer;
    6.     newPlayer = (GameObject)Instantiate(playerNetwork);
    7.  
    8.     NetworkObject netObj = newPlayer.GetComponent<NetworkObject>();
    9.     newPlayer.SetActive(true);
    10.     netObj.SpawnAsPlayerObject(clientId, true);
    11. }
    12.  
    This is where I call the method:
    Code (CSharp):
    1. if (PlayerNetworkController.instance == null)
    2. {
    3.     Debug.Log("Player does not exists.");
    4.     if (IsServer)
    5.     {
    6.         Debug.Log("Is server. Creating server player");
    7.         SpawnPlayerServerRpc(0);
    8.     } else {
    9.         Debug.Log("Is not server. Creating client player");
    10.         SpawnPlayerServerRpc(1);
    11.     }
    12. } else {
    13.     Debug.Log("Player already exists.");
    14. }
    Why does this only ever create the host prefab? Everything apart from that works - the scene gets loaded for the client and you can even see the host prefab moving, but the client does not get its own prefab. It reaches the appropriate Debug.Logs, too, so there must be another issue.
     
  2. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    440
    Hi @Michel3033 , I'd liek to help but there are a few parts of your question I'm not getting.

    1. What NGO version are you using?
    2. Where does the 2nd come come from? is it part of something else? When is it called?
    3. What do you mean by "It reaches the appropriate Debug.Logs"?