diff --git a/Assets/Mirror/Runtime/NetworkManager.cs b/Assets/Mirror/Runtime/NetworkManager.cs
index 324a6f850..845e53c7f 100644
--- a/Assets/Mirror/Runtime/NetworkManager.cs
+++ b/Assets/Mirror/Runtime/NetworkManager.cs
@@ -273,15 +273,10 @@ bool IsServerOnlineSceneChangeNeeded()
return !string.IsNullOrEmpty(onlineScene) && onlineScene != loadedSceneName && onlineScene != offlineScene;
}
- ///
- /// This starts a new server.
- /// This uses the networkPort property as the listen port.
- ///
- ///
- public void StartServer()
+ // full server setup code, without spawning objects yet
+ void SetupServer()
{
- if (LogFilter.Debug) Debug.Log("NetworkManager StartServer");
-
+ if (LogFilter.Debug) Debug.Log("NetworkManager SetupServer");
InitializeSingleton();
if (runInBackground)
@@ -312,6 +307,16 @@ public void StartServer()
RegisterServerMessages();
isNetworkActive = true;
+ }
+
+ ///
+ /// This starts a new server.
+ /// This uses the networkPort property as the listen port.
+ ///
+ ///
+ public void StartServer()
+ {
+ SetupServer();
// scene change needed? then change scene and spawn afterwards.
if (IsServerOnlineSceneChangeNeeded())
@@ -394,13 +399,15 @@ public void StartClient(Uri uri)
///
public virtual void StartHost()
{
+ // setup server first
OnStartHost();
+ SetupServer();
- // SetupLocalConnection needs to be called BEFORE StartServer:
+ // SetupLocalConnection needs to be called BEFORE SpawnObjects:
// https://github.com/vis2k/Mirror/pull/1249/
// -> this sets NetworkServer.localConnection.
- // -> localConnection needs to be set before StartServer because:
- // -> StartServer calls OnStartServer
+ // -> localConnection needs to be set before SpawnObjects because:
+ // -> SpawnObjects calls OnStartServer in all NetworkBehaviours
// -> OnStartServer might spawn an object and set [SyncVar(hook="OnColorChanged")] object.color = green;
// -> this calls SyncVar.set (generated by Weaver), which has
// a custom case for host mode (because host mode doesn't
@@ -421,7 +428,20 @@ public virtual void StartHost()
// -> localClientActive needs to be true, otherwise the hook
// isn't called in host mode!
NetworkClient.SetupLocalConnection();
- StartServer();
+
+ // scene change needed? then change scene and spawn afterwards.
+ if (IsServerOnlineSceneChangeNeeded())
+ {
+ ServerChangeScene(onlineScene);
+ }
+ // otherwise spawn directly
+ else
+ {
+ NetworkServer.SpawnObjects();
+ }
+
+ // connect client and call OnStartClient AFTER any possible server
+ // scene changes.
ConnectLocalClient();
OnStartClient();
}