Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Only one player for the host but two for the client

Discussion in 'Netcode for GameObjects' started by Deleted User, Feb 28, 2022.

  1. Deleted User

    Deleted User

    Guest

    Hello,
    I recently try the Netcode SDK, i've followed drapperdino tutorial, and when i click on start the game i've created a very simple script in a gameobject called GameManager (object & script).
    But when i start the game the client see 2 players but the host see only one players.
    I've added the player prefab (literally a capsule with a camera behind) to Networked Prefab in NetworkManager.
    And finally i've added a NetworkObject to the Player Prefab.
    P.S.I don't use the PlayerPrefab option in NetworkManager.

    Here is my GameManager.cs
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using Unity.Netcode;
    4. using UnityEngine;
    5.  
    6. public class GameManager : NetworkBehaviour
    7. {
    8.  
    9.     public GameObject PlayerPrefab;
    10.     public float x1;
    11.     public float x2;
    12.     public float y;
    13.     public float z1;
    14.     public float z2;
    15.  
    16.     void Start()
    17.     {
    18.         Spawn();
    19.     }
    20.  
    21.     // Update is called once per frame
    22.     void Update()
    23.     {
    24.        
    25.     }
    26.  
    27.     private void Spawn()
    28.     {
    29.         GameObject SpawnBlock = GameObject.Find("StartLine");
    30.         if (SpawnBlock == null)
    31.         {
    32.             Debug.LogError("Can't find starting line !");
    33.         }
    34.         else
    35.         {
    36.             x2 = (SpawnBlock.transform.localScale.x / 2) - 1;
    37.             x1 = x2 * -1;
    38.             y = SpawnBlock.transform.position.y + 1.5f;
    39.             z2 = (SpawnBlock.transform.localScale.z / 2) - 1;
    40.             z1 = z2 * -1;
    41.             GameObject Player = Instantiate(PlayerPrefab, new Vector3((float)System.Math.Round(Random.Range(x1, x2), 0), y, (float)System.Math.Round(Random.Range(z1, z2), 0)), Quaternion.identity);
    42.             Player.GetComponent<NetworkObject>().Spawn();
    43.             Player.name = "Player_" + PlayerPrefs.GetString("PlayerName");
    44.  
    45.         }
    46.     }
    47. }
    48.  
    If someone help me
     
  2. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    When the host connects, the code runs as expected. However when the client connects, it gets the host player object spawned automatically, and the code you posted instantiates a player object for the client – however, Start is only called on the new client, not on the server, and the server is the only one that can spawn an object over the network. You therefore need to make sure that Spawn runs on the server, for the client, and not on the client itself.
     
  3. Deleted User

    Deleted User

    Guest

    So where do i need to put the script ?
     
  4. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    You have many options, but most commonly you would make Spawn() a ServerRpc that is called by newly connected clients. Putting a call to that in Start() might work, but I would put it in OnNetworkSpawn() to be safe. See also this thread for examples.
     
  5. Deleted User

    Deleted User

    Guest

    I replaced Start() Void by
    Code (CSharp):
    1. public override void OnNetworkSpawn()
    2.     {
    3.         Spawn();
    4.     }
    And now it doesn't work, nobody spawn
     
  6. CosmoM

    CosmoM

    Joined:
    Oct 31, 2015
    Posts:
    204
    Is GameManager not a NetworkObject? I recommend you reread my post and the linked thread – you need to make sure that Spawn() is a ServerRpc, only the server should be instantiating and spawning players.
     
  7. Deleted User

    Deleted User

    Guest

    Now i have a NullReferenceException: Object reference not set to an instance of an object
    For this part of code:
    Code (CSharp):
    1.  
    2. [ServerRpc]
    3.     private void SpawnServerRpc()
    4.     {
    5.  
    6.         MainCamera.SetActive(false);
    7.         GameObject SpawnBlock = GameObject.Find("StartLine");
    8.         if (SpawnBlock == null)
    9.         {
    10.             Debug.LogError("Can't find starting line !");
    11.         }
    12.         else
    13.         {
    14.             x2 = (SpawnBlock.transform.localScale.x / 2) - 1;
    15.             x1 = x2 * -1;
    16.             y = SpawnBlock.transform.position.y + 1.5f;
    17.             z2 = (SpawnBlock.transform.localScale.z / 2) - 1;
    18.             z1 = z2 * -1;
    19.             GameObject Player = Instantiate(PlayerPrefab, new Vector3((float)System.Math.Round(UnityEngine.Random.Range(x1, x2), 0), y, (float)System.Math.Round(UnityEngine.Random.Range(z1, z2), 0)), Quaternion.identity);
    20.             Player.GetComponent<NetworkObject>().Spawn();
    21.             Player.name = "Player_" + PlayerPrefs.GetString("PlayerName");
    22.  
    23.  
    24.         }
    25.     }
    26.  
    And I call this function in Start()
    What's wrong in my code ?
     
    Last edited by a moderator: Mar 3, 2022