Starting and Stopping Network
Learn how to start, stop, and reset the network connection using ObjectNet's NetworkManager.
Starting the Network:
To perform any networked operation—such as sending RPCs or synchronizing data—you must first start the network. This requires a running server, host, or client instance.
ObjectNet provides both automatic startup options through the NetworkManager
and full manual control via code if you prefer runtime configuration.
- Automatic Startup (Inspector)
In the NetworkManager
component, you can configure the connection mode and startup behavior directly in the inspector: Connections
Connection Mode: Choose between
Server
,Client
.Connect on Start: If enabled, the network will start automatically when the scene loads.
- Manual Startup
If you uncheck Connect on Start, you can start the network manually at any point in your code using:
NetworkManager.Instance().StartNetwork();
This will initialize the network using the selected Connection Mode from the NetworkManager
.
- Manual Mode (Runtime Configuration)
For full control, you can use Manual Mode, which allows you to decide at runtime whether to act as a server or client. This is useful for letting players choose how they want to connect.
string ipAddress = NetworkManager.Instance().GetPrivateIp();
// Configure the network manager to operate in server mode.
NetworkManager.Instance().ConfigureMode(NetworkConnectionType.Server);
NetworkManager.Instance().SetServerAddress(ipAddress);
NetworkManager.Instance().StartNetwork();
//let the client input the server ip manually, for example through a UI input field
string serverIpAdress = inputfield.text;
// Configure the network manager to operate in client mode.
NetworkManager.Instance().ConfigureMode(NetworkConnectionType.Client);
NetworkManager.Instance().SetServerAddress(serverIpAdress);
NetworkManager.Instance().StartNetwork();
Stopping the Network:
To gracefully stop an active network session, call:
NetworkManager.Instance().StopNetwork();
//Note:For steam transport use this instead
//(this will both stop the network and leave the steam lobby)
NetworkSteamManager.Instance().LeaveLobby();
If called by the server or host, it will disconnect all clients and shut down the entire session.
If called by a client, it will only disconnect that client from the server without affecting others.
After stopping the network, it is recommended (but optional) to destroy the NetworkManager instance, especially if you plan to reload scenes or restart the session. This ensures all network-related values are reset cleanly. example:
NetworkManager.Instance().StopNetwork();
Destroy(NetworkManager.Instance().gameObject);
SceneManager.LoadScene("MainMenuScene");
Last updated