Search Unity

Question SceneOriginHandle is still zero but the NetworkObject is already spawned!

Discussion in 'Multiplayer' started by Malloric, Dec 11, 2022.

  1. Malloric

    Malloric

    Joined:
    Nov 19, 2019
    Posts:
    2
    Hello everyone,

    This has been cracking my head and I've ran out of ideas, even with the internet's help. I'm trying to add multiplayer to our game and currently I've hit a rock quite early.

    Exception: GetSceneOriginHandle called when SceneOriginHandle is still zero but the NetworkObject is already spawned!

    That is the error I get when I make a build, run it as a server and try to connect my editor to it as a client. The entrypoint is in the function StartClient():


    Code (CSharp):
    1. public class OnlineGUI : NetworkBehaviour
    2. {
    3.     [SerializeField] public GameObject prefab;
    4.  
    5.     void OnGUI()
    6.     {
    7.         GUILayout.BeginArea(new Rect(10, 10, 300, 300));
    8.         if (!NetworkManager.Singleton.IsClient && !NetworkManager.Singleton.IsServer)
    9.         {
    10.             StartButtons();
    11.         }
    12.         else
    13.         {
    14.             StatusLabels();
    15.         }
    16.  
    17.         GUILayout.EndArea();
    18.     }
    19.  
    20.     void StartButtons()
    21.     {
    22.         if (GUILayout.Button("Host")) NetworkManager.Singleton.StartHost();
    23.         if (GUILayout.Button("Client")) StartClient();
    24.         if (GUILayout.Button("Server")) NetworkManager.Singleton.StartServer();
    25.     }
    26.  
    27.     static void StatusLabels()
    28.     {
    29.         var mode = NetworkManager.Singleton.IsHost ?
    30.             "Host" : NetworkManager.Singleton.IsServer ? "Server" : "Client";
    31.  
    32.         GUILayout.Label("Transport: " +
    33.             NetworkManager.Singleton.NetworkConfig.NetworkTransport.GetType().Name);
    34.         GUILayout.Label("Mode: " + mode);
    35.     }
    36.  
    37.  
    38.     void StartClient()
    39.     {
    40.         NetworkManager.Singleton.StartClient();
    41.         SpawnPlayerCharacterServerRpc();
    42.     }
    43.  
    44.  
    45. public override void OnNetworkSpawn()
    46.     {
    47.         SpawnPlayerCharacterServerRpc();
    48.     }
    49.  
    50.     [ServerRpc(RequireOwnership = false)]
    51.     void SpawnPlayerCharacterServerRpc()
    52.     {
    53.         GameObject go = Instantiate(prefab, new Vector3(-6.0f, 0.5002056f, 0.0f), Quaternion.identity);
    54.         go.GetComponent<NetworkObject>().Spawn();
    55.     }
    56.  
    57.  
    I have also tried allowing for it to be the NetworkManager to spawn a selected player prefab, but the error remains. Any ideas or suggestions? Thanks!
     
    Last edited: Dec 11, 2022
  2. Malloric

    Malloric

    Joined:
    Nov 19, 2019
    Posts:
    2
    Hey,

    For anyone with this issue, this happened because I started my server on the awake function...I do not recommend that!
    Closing this.
     
    BSimonSweet likes this.