Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice

Question Starting client from menu scene and proceed with other scene

Discussion in 'Multiplayer' started by ben_carrick, Aug 22, 2021.

  1. ben_carrick

    ben_carrick

    Joined:
    Apr 10, 2020
    Posts:
    2
    Hi everyone,
    I'm trying to get my head around how to start my client in one scene and then move over to another one.

    Here is my setup:
    I got a menu scene where I handle the setup of the client and host. Connection parameters, error messages, etc.

    The actual game scene has a script that will spawn the player figures on startup and looks like this.

    Code (CSharp):
    1. public class SessionStartup : NetworkBehaviour
    2. {
    3.     [SerializeField]
    4.     private GameObject playerPrefab;
    5.  
    6.     void Start()
    7.     {
    8.         if (IsServer)
    9.         {
    10.             this.GetComponent<NetworkObject>().Spawn();
    11.         }
    12.         SpawnPlayerServerRpc(NetworkManager.Singleton.LocalClientId);
    13.     }
    14.  
    15.     [ServerRpc(RequireOwnership = false)]
    16.     private void SpawnPlayerServerRpc(ulong clientId)
    17.     {
    18.         GameObject go = Instantiate(playerPrefab, Vector3.zero, Quaternion.identity);
    19.         go.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId,null,false);
    20.     }
    21. }
    This works fine for the host, but not for the client.
    The StartClient() method is called in the Menu scene. This is necessary so that I can show error messages when the connection failed. If the client connection is successful I proceed to the game scene. The problem now is, that the player will be instantiated immediately in the menu scene. After I proceed to the next scene, it will be destroyed and on the client I'll only see myself while on the server I see both players.

    Is there a possibility to delay the synchronization with the server, so all network objects will be instantiated after I loaded the game scene?