NetworkBehaviour

NetworkBehaviours are a fundamental part of networking which allow you to easily synchronize data and access network related information.

When inheriting from NetworkBehaviours you are indicating that your script will utilize the network in some way. Once a NetworkBehaviour script is added to an object the NetworkObject component will automatically be attached.

NetworkBehaviours are essential for Remote Procedure Calls, Synchronizing, and having access to vital network information.


Properties

There are several public properties available within NetworkBehaviour, many of which you will use regularly. Most of the properties are only available after the object has been initialized. See the callbacks section for more information on initialization.

Commonly used properties are:

  • IsClientInitialized will be true if acting as a client and object is network initialized.

  • IsServerInitialized will be true if acting as the server and object is network initialized.

  • IsOwner is true if you are a client, and the owner of the object.

  • HasAuthority is true if you are the owner, or if server and there is no owner.

To view all properties please visit the NetworkBehaviour API.

When accessing a NetworkBehaviour property or method consider using the 'base' keyword to show your intentions.

For example, base.IsOwner


Callbacks

Like Properties, there are a large number of available methods. The methods you will be using commonly are known as callbacks. If you are interested in all available methods view the methods section under API.

Most callbacks have a server and client version, and each has great potential. To use a callback you must inherit from NetworkBehaviour and override the callback you wish to use.

The execution order of spawn related callbacks is constant, but some callbacks may occur at any time. Included is a chart to help you remember callback order.

Created by Winterbolt

To begin we will cover the server side callbacks.

OnStartNetwork

In some instances you will need to initialize for both server and client. You can save some code and time by using OnStartNetwork instead of the OnStart for Client and Server. It's important to remember that OnStartNetwork will only call once, even if you are clientHost.

OnStartServer

When clientHost base.IsOwner will not return true, even with the owner set. This is because IsOwner is a client-side check, and the client has not yet initialized the object.

When you need to check 'IsOwner' on OnStartServer use base.Owner.IsLocalClient.

OnOwnershipServer

OnSpawnServer

OnDespawnServer

When the server despawns a NetworkObject any pending synchronize changes will be sent out with the despawn message. This ensures clients will get the latest data, even if timed, before the object is despawned for them.

OnStopServer

Next are the client callbacks. These callbacks will always occur after the server callbacks, even when acting as host. For example, a callback sequence might look like this: OnStartServer, OnOwnershipServer, OnSpawnServer, OnStartClient, OnOwnershipClient.

For the most part client callbacks are the same as the server ones, except they occur only if the client connection is started. Only key differences will be included in the descriptions of each callback.

OnStartClient

Any buffered remote procedure calls will occur immediately after OnStartClient.

If you are coming from Mirror instead of using OnLocalPlayer use OnStartClient with a base.IsOwner check.

OnOwnershipClient

OnStopClient

OnStopNetwork

Like OnStartNetwork, this method can be used to save lines of code when needing to deinitialize for both server and client stop. OnStopNetwork will also only be called once even when clientHost.

Last updated