Search Unity

Objects spawning twice on server side

Discussion in 'Multiplayer' started by Temeos, Nov 9, 2022.

  1. Temeos

    Temeos

    Joined:
    Mar 29, 2019
    Posts:
    22
    Hello!

    I have a problem in my project that I can't quite wrap my head around.

    Basically I have a local multiplayer, where one player is the host and the other is a client. I have a secene where two players can join a lobby and spawn into the main scene from there once they are ready. This works fine. The problem starts when the players spawn in.

    I am spawning the players on the same spot and spawn two "Bot" prefabs in front of the player. The bots spawn twice on the host but only one time for the client.

    I tried using the [Client] attribute for the spawning method and also the [Server] Attribute, but neither yielded the result I wanted.

    Here is some (pseudo) code to represent what I am doing.

    In custom NetworkManager:

    Code (CSharp):
    1. public override void OnServerReady(NetworkConnectionToClient conn)
    2. {
    3.        base.OnServerReady(conn)
    4.  
    5.        OnServerReadied?.Invoke(conn)
    6.        OnServerReadiedBot?.Invoke(true)
    7. }
    On scene change I spawn in a prefab with the SpawnSystem script attached to it, which has the following relevant methods:


    Code (CSharp):
    1. public override void OnStartServer()
    2. {
    3.    NetworkManager.OnServerReadied += SpawnPlayer
    4.    if(scene == targetScene)
    5.    {
    6.        NetworkManager.OnServerReadiedBot += SpawnNPC
    7.    }
    8.    base.OnServerStart()
    9. }
    Here i tried the Attributes.

    Code (CSharp):
    1. public void SpawnPlayer(NetworkConnection conn)
    2.     {
    3.         Transform spawnPoint = spawnPoints.ElementAtOrDefault(nextIndex);
    4.  
    5.         if (spawnPoint == null)
    6.         {
    7.             Debug.LogError($"Missing spwan point for Player {nextIndex}");
    8.             return;
    9.         }
    10.  
    11.         if (SceneManager.GetActiveScene().name == "BaseLine")
    12.         {
    13.             InstantiateRandomPlayer(conn, true);
    14.         }                
    15.         else
    16.         {
    17.             InstantiateRandomPlayer(conn);
    18.             nextIndex++;
    19.         }
    20.  
    21.     }
    I tried the attributes here as well.

    Code (CSharp):
    1. public void SpawnNPC(bool blActive = false)
    2.     {
    3.         //Some Position Setttings
    4.         GameObject npcInstance = new GameObject();
    5.         GameObject[] npcsBL = new GameObject[2];
    6.  
    7.         if (!blActive)
    8.         {
    9.             npcInstance = Instantiate(npc, npcPosDynamic, spawnPoints[nextIndex].rotation);
    10.             NetworkServer.Spawn(npcInstance);
    11.         }
    12.         else
    13.         {
    14.  
    15.             for (int i = 0; i <= 1; i++)
    16.             {
    17.                 npcsBL[i] = Instantiate(npcBL, npcPos[i], spawnPoints[i + 1].rotation);
    18.                 NetworkServer.Spawn(npcsBL[i]);
    19.             }            
    20.         }
    21.         nextIndex++;
    22.     }
    Code (CSharp):
    1.  private void InstantiateRandomPlayer(NetworkConnection conn, bool blActive = false)
    2.     {
    3.         if (blActive)
    4.         {
    5.             GameObject blPlayerInstance = Instantiate(blPlayer, spawnPoints[0].position,                  spawnPoints[0].rotation);
    6.             NetworkServer.Spawn(blPlayerInstance, conn);
    7.         }
    8.         else
    9.         {
    10.             var prefabIndex = Random.Range(0, 2);
    11.             GameObject playerInstance = Instantiate(playerPrefabs[prefabIndex], spawnPoints[nextIndex].position, spawnPoints[nextIndex].rotation);
    12.             NetworkServer.Spawn(playerInstance, conn);
    13.         }
    14.     }
    Any help would be apprerciated!
     
  2. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    The host is usually both client and server, that could be the reason of the bot spawning twice.
     
  3. Temeos

    Temeos

    Joined:
    Mar 29, 2019
    Posts:
    22
    But shouldn't the [Client] Attribute for the spawning functions fix the problem in that case?
     
  4. r31o

    r31o

    Joined:
    Jul 29, 2021
    Posts:
    460
    As I said, the host is both server and client, so the client attribute wont do anything. You should check if you are the host on one of the spawn functions and return if true
     
    Temeos likes this.