When using the Network Manager, a player is added by default when a client connects to the server. Players are represented by designated GameObjects (See Unity documentation on Player GameObjects to learn more). The player GameObject exists on both the Server and the Client instances.
Different callbacks are received depending on your game’s connection state. These can be set in the Network Manager HUD.
Three connection states are available:
Host: To set your game up in Host mode, select LAN Host(H).
Client: To set your game up in Client mode, select LAN Client(C) and enter the server IP into the text field or use the API function StartClient()
. Note that your server IP must be in the same local network - or you can type “localhost” if you are connecting to the same machine.
Server: To set your game up in Server mode, select LAN Server Only(S).
During run time, the Network Manager HUD’s controls are also available in the Network Manager HUD component’s Inspector window. Click Runtime Controls to access these.
The callbacks in this section are defined in the NetworkBehaviour class and are called only on the player GameObject. To learn more about setting up game states, see Using the Network Manager: Game state management.
Some callbacks require you to have multiple instances of the game running (for example, two Standalone instances, or one Standalone and one in the Editor). These instances can be on the same machine or on different machines, as long as those machines are connected through a local network. This also works in the same way for MatchMaker, but with MatchMaker your machines can also be connected via the Internet.
To get these callbacks you need to have two instances of the game, one running in Server mode and the other running in Client mode. These callbacks are only called on the player GameObject on the Server instance.
Launch the Server mode instance first, then start the Client instance to get the callbacks:
OnStartServer
OnRebuildObservers
(Start()
function is called)
To get these callbacks you need to have two instances of the game, one running in Server mode and the other running in Client mode. These callbacks are only called on the player GameObject on the Client instance.
Launch the Server mode instance first, then start the Client instance to get the callbacks:
OnStartClient
OnStartLocalPlayer
OnStartAuthority
(Start()
function is called)
To get these callbacks you need to have one instance of the game running in Host mode, either in the Editor or as a Standalone build. These callbacks are only called on the Player GameObject:
OnStartServer
OnStartClient
OnRebuildObservers
OnStartAuthority
OnStartLocalPlayer
(Start()
function is called)
OnSetLocalVisibility
To get the OnNetworkDestroy
callback you need to have three Standalone instances of the game, one running in Server mode and two running in Client mode.
Follow the instructions below to get the OnNetworkDestroy
callback:
Start Server instance
Start Client instance
Start a second Client instance
When both Client instances have automatically connected to the Server instance, stop one Client instance
OnNetworkDestroy
is then called on the remaining Client instance, but not on the Server instance