Skip to content

Network Instantiation Flush

Peter Halasz edited this page Apr 21, 2019 · 4 revisions

After creating or joining a server it is necessary to call NetworkObject.Flush on the related NetWorker. This is to notify Forge that unity is ready and it is possible to spawn NetworkBehaviors into the scene.

The function gets automatically called by the NetworkManager when you switch scenes if "Automatic Scenes" is ticked on said NetworkManager. (This is also what happens during the default MultiplayerMenu).

On the server:

        int maxAllowedClients = 32;
        UDPServer server = new UDPServer(maxAllowedClients);
        server.Connect(port:8888);
        NetworkManager.Instance.Initialize(server);
        NetworkObject.Flush(server);

On the client when not switching scenes manually:

    client.serverAccepted += OnAccepted;

    ...

    public void OnAccepted(NetWorker sender)
    {
        NetworkObject.Flush(sender);
    }

If you are manually loading new scenes then Flush needs to be called every time when the new scene loaded both on the server and the client. The easiest is to subscribe to Unity's SceneManager.sceneLoaded event. Optionally you could use the NetworkManager.SceneReady method just like it would be used when "Automatic Scenes" is turned on.

Home

Getting Started
Network Contract Wizard (NCW)
Network Object
Remote Procedure Calls (RPCs)
Unity Integration
Basic Network Samples
Scene Navigation
NetWorker
Master Server
Web Server
Netcoding Design Patterns
Troubleshooting
Miscellaneous
Clone this wiki locally
You can’t perform that action at this time.