Mirror
  • Mirror Networking
  • API Reference
  • Development Blog
    • A Brief History of Mirror
  • User Manual
    • General
      • Getting Started
      • Script Templates
      • Change Log
        • 2024 Change Log
        • 2023 Change Log
        • 2022 Change Log
        • 2021 Change Log
        • 2020 Change Log
        • 2019 Change Log
      • Deprecations
      • Migration Guide
      • Integrations
      • Timestamp Batching
      • TCP and UDP
      • CCU
      • SyncDirection
      • Round Trip Time (RTT)
      • Connection Quality
      • Lag Compensation
      • Client Side Prediction
      • History Bounds
      • Tests
      • NetGraph
    • FAQ
      • Execution Order
    • Transports
      • KCP Transport
      • Telepathy Transport
      • WebSockets Transport
        • Reverse Proxy
          • Windows
            • IIS
          • Linux
            • NGINX
            • Caddy
            • Apache
            • HA Proxy
        • SSL
      • Multiplex Transport
      • Latency Simulation Transport
      • Ignorance
      • LiteNetLib Transport
      • FizzySteamworks Transport
      • FizzyFacepunch Transport
      • Encryption Transport
      • Edgegap Transports
        • Edgegap Relay
        • Edgegap Lobby
    • Components
      • Network Animator
      • Network Authenticators
        • Basic Authenticator
        • Device Authenticator
      • Network Behaviour
      • Network Discovery
      • Network Identity
      • Network Manager
      • Network Manager HUD
      • Network Ping Display
      • Network Profiler
      • Network Rigidbody
      • Network Lerp Rigidbody
      • Network Room Manager
      • Network Room Player
      • Network Start Position
      • Network Statistics
      • Remote Statistics
      • Network Transform
        • Snapshot Interpolation
      • Deprecated
        • Network Proximity Checker
        • Network Scene Checker
        • Network Match Checker
        • Network Owner Checker
    • Interest Management
      • Spatial Hashing
      • Distance
      • Scene
      • Scene + Distance
      • Match
      • Team
      • Custom
      • Legacy
    • Guides
      • Authority
      • IDs
      • Attributes
      • Time Synchronization
      • Data types
      • Serialization
      • Synchronization
        • SyncVars
        • SyncVar Hooks
        • SyncEvent (Obsolete)
        • SyncLists
        • SyncDictionary
        • SyncHashSet
        • SyncSortedSet
      • Communications
        • Remote Actions
        • NetworkManager Callbacks
        • NetworkBehaviour Callbacks
        • Network Messages
      • GameObjects
        • Player Game Objects
        • Custom Character Spawning
        • Custom Spawn Functions
        • Scene GameObjects
        • Pickups, Drops, and Child Objects
    • Examples
      • Additive Levels
      • Additive Scenes
      • Basic
      • Billiards
      • Multiple Additive Scenes
      • Pong
      • Room
      • Tanks
      • EdgegapLobby
  • Server Hosting
    • The Pragmatic Hosting Guide
    • Cloud Hosting Guides
      • AWS
      • Google Cloud
      • Oracle Free Tier
    • Hosting with a Remote Desktop
    • Edgegap Hosting Plugin Guide
  • Security
    • Security Overview
    • Cheat Protection Stages
    • Cheats & Anticheats
  • Community Guides
    • Community Translations
    • Video Tutorials
    • Resources
    • Mirror Quick Start Project
    • Unity for MMORPGs
    • Unity Canvas HUD
    • Odin Inspector Support
    • Ready Up And Die!
    • iOS AppStore
    • Mirror Docker Guide
    • Gitbook Guide
    • Mirror Branding
    • Contributors Agreement
    • Documentation License
Powered by GitBook
On this page
  • Properties
  • Network Callbacks
  • Server and Client Attributes
  • Commands
  • Client RPC Calls
  • Networked Events (Obsolete)
  1. User Manual
  2. Components

Network Behaviour

PreviousDevice AuthenticatorNextNetwork Discovery

Last updated 1 year ago

See also in the API Reference.

Network Behaviour scripts work with game objects that have a Network Identity component. These scripts can perform high-level API functions such as Commands, ClientRpc's, and SyncVars.

Do not put objects in DontDestroyOnLoad (DDOL) in Awake. You can do that in Start instead.

With the server-authoritative system of Mirror, the server must use the NetworkServer.Spawn function to spawn game objects with Network Identity components. Spawning them this way assigns them a netId and creates them on clients connected to the server.

Note: This is not a component that you can add to a game object directly. Instead, you must create a script which inherits from NetworkBehaviour (instead of the default MonoBehaviour), then you can add your script as a component to a game object.

Properties

  • isServer Returns true in server context if this game object has been spawned.

  • isClient Returns true in client context if this game object has been spawned by the server.

  • isLocalPlayer Returns true on the client if this game object represents the player created for this client.

  • isOwned (formerly hasAuthority) Returns true on the client if this client has over this game object. It is meaningless in server context.

  • netId The unique network ID of this game object. The server assigns this at run time. It is unique for all game objects on the network.

  • netIdentity Returns the Network Identity of this object

  • connectionToServer The Network Connection associated with the Network Identity component attached to this game object. This is only valid for the local player object on the client, and is null for other player objects that may exist on the client.

  • connectionToClient The Network Connection associated with the Network Identity component attached to this game object. This is valid for game objects on the server that have been assigned to a specific client, e.g. player objects, pets, henchmen, or other objects a single client "owns".

Network Behaviour scripts have the following features:

  • Synchronized variables

  • Network callbacks

  • Server and client functions

  • Sending commands

  • Client RPC calls

  • Networked events

Network Callbacks

There are built-in callback functions which are invoked on Network Behaviour scripts for various network events. These are virtual functions on the base class, so you can override them in your own code like this:

public class SpaceShip : NetworkBehaviour
{
    public override void OnStartServer()
    {
        // disable client stuff
    }

    public override void OnStartClient()
    {
        // register client events, enable effects
    }
}

The built-in callbacks are:

  • OnStartServer called on server when a game object spawns on the server, or when the server is started for game objects in the Scene

  • OnStopServer called on server when a game object is destroyed on the server, or when the server is stopped for game objects in the Scene

  • OnStartClient called on clients when the game object spawns on the client, or when the client connects to a server for game objects in the Scene

  • OnStopClient called on clients when the server destroys the game object

  • OnStartLocalPlayer called on clients after OnStartClient for the player game object on the local client

  • OnStopLocalPlayer called on clients before OnStopClient for the player game object on the local client

  • OnStartAuthority called on owner client when assigned authority by the server. isOwned will be true for such objects in client context.

  • OnStopAuthority called on owner client when authority is removed by the server.

Note that in a peer-hosted setup, when one of the clients is acting as both host and client, both OnStartServer and OnStartClient are called on the same game object. Both these functions are useful for actions that are specific to either the client or server, such as suppressing effects on a server, or setting up client-side events.

Server and Client Attributes

You can tag methods in Monobehaviour and Network Behaviour scripts with custom attributes to designate them as server-only or client-only functions. [Server] and [ServerCallback] return immediately if the client is not active. Likewise, [Client] and [ClientCallback] return immediately if the server is not active.

The [Server] and [Client] attributes do not generate compile time errors, but they do emit a warning log message if called in the wrong context.

The ServerCallback and ClientCallback attributes act the same as above but do not cause a warning to be generated.

Commands

To execute code on the server, you must use Commands. The high-level API is a server-authoritative system, so Commands are the only way for a client to trigger some code on the server.

When a client player game object sends a command, that command runs on the corresponding player game object on the server. This routing happens automatically, so it is impossible for a client to send a command for a different player.

To define a Command in your code, you must decorate a method with the [Command] attribute. While not required, it is strongly suggested to prefix method names with Cmd so it's easier to recognize in your calling code what will be happening.

Commands are called just by invoking the method normally on the client. Instead of the method running on the client, it is automatically invoked on the corresponding player game object on the server.

Commands are type-safe, have built-in security and routing to the player, and use an efficient serialization mechanism for the arguments to make calling them fast.

Client RPC Calls

Client RPC calls are a way for server game objects to make things happen on client game objects.

Client RPC calls are not restricted to player game objects, and may be called on any game object with a Network Identity component.

To define a Client RPC call in your code, you must decorate the method with [ClientRpc]. While not required, it is strongly suggested to prefix method names with Rpc so it's easier to recognize in your calling code what will be happening.

Networked Events (Obsolete)

Networked events are like Client RPC calls, but instead of calling a function on the game object, they trigger Events instead.

This allows you to write scripts which can register for a callback when an event is triggered.

To define a Networked event in your code, you must write a function which both:

  • Has a name that begins with Event

  • Has the SyncEvent attribute

You can use events to build powerful networked game systems that can be extended by other scripts. This example shows how an effect script on the client can respond to events generated by a combat script on the server.

SyncEvent is the base class that Commands and ClientRpc calls are derived from. You can use the SyncEvent attribute on your own functions to make your own event-driven networked game play code. Using SyncEvent, you can extend Mirror’s Multiplayer features to better fit your own programming patterns.

For more information, see .

Typically only player objects can have Commands, however they may also exist on other networked objects, and may be called by a client that has authority assigned for that object, or by any client where has been specifically indicated.

See and related sections for more information.

See and related sections for more information.

IMPORTANT SyncEvents have been removed in version 18.0.0, see this for more information

See for more details.

NetworkBehaviour
authority
Attributes
bypassing authority
Communications
Communications
Issue
SyncEvents