Skip to main content

NetworkBehaviour

NetworkBehaviour is an abstract class that derives from MonoBehaviour and is the base class all your networked scripts should derive from. Each NetworkBehaviour is owned by a NetworkObject.

note

GameObjects, NetworkObjects and NetworkBehaviour aren't serializable types so they can't be used in RPCs or NetworkVariables by default. For infromation on how to send a reference to a NetworkBehaviour over RPCs or NetworkVariables see NetworkObject &NetworkBehaviour.

NetworkBehaviours can contain RPC methods and NetworkVariables. When you call an RPC function, the function isn't called locally. Instead a message is sent containing your parameters, the networkId of the NetworkObject that owns the NetworkBehaviour that the Invoke was called on, and the "index" of the NetworkBehaviour on the NetworkObject. This means for multi project setups (one project for the server and another one for the client), it's important that the order and amount of NetworkBehaviours on each NetworkObject is the same. It also means that NetworkBehaviours can only exist as a child or on the same object as a NetworkObject that is actively Spawned.

You can have multiple NetworkBehaviors on the same object and on any child object. Each NetworkBehaviour belongs to a NetworkObject. It will be the first parent or first component on the current object that is found. You can only have one NetworkObject at the root of the prefab.

NetworkStart is called on each behavior related to the NetworkObject.

Inside a NetworkBehaviour you can use NetworkVariable and RPCs to synchronize state and send messages over the network.