NetworkServer not available inside NetworkManger.OnStartServer??

It seems the NetworkServer instance is not yet active inside NetworkManager.OnStartServer, so if I am trying to call NetworkServer.Spawn inside OnStartServer, I get the following exception:

SpawnObject for xxxx (UnityEngine.GameObject), NetworkServer is not active. Cannot spawn objects without an active server.

NetworkServer seems to be only usable after the StartServer call complete finishes, so for example, the following sequence works:

NetworkManager.StartServer();
NetworkServer.Spawn(myObject);

However, as I am calling NetworkManager.StartServer at various places, and I have some common server object creation methods that I would like to be hooked into a common place such as OnStartServer, and I also want to spawn the objects through NetworkServer, which miserably fails.

Any suggestions on how can I get around this issue?

1 Like

I just wanted to leave a reply that i am also having this issue. It would seem that the OnStartServer hook gets called before the server is active.

Even if i try to call something like a loading() function from within networkmanager.onstartserver and do my spawning there, i still get the error.

Anyone know of a workaround?

Use Coroutine and while (true).

    /// <summary>
    /// Load map hook;
    /// </summary>
    /// <param name="sceneName"></param>
    public override void OnServerSceneChanged(string sceneName)
    {
        if (sceneName == "scene")
        {
            ServerData.LoadServer();
            // spawn, etc
        }
    }