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
  • NetworkBehaviour Callbacks
  • Server Only
  • Client only
  • Example flows
  • Server mode
  • Client mode
  • Host mode
  1. User Manual
  2. Guides
  3. Communications

NetworkBehaviour Callbacks

PreviousNetworkManager CallbacksNextNetwork Messages

Last updated 1 year ago

NetworkBehaviour Callbacks

See also in the API Reference.

There are a number of events relating to network behaviours that can occur over the course of a normal multiplayer game. These include events such as the host starting up, a player joining, or a player leaving. Each of these possible events has an associated callback that you can implement in your own code to take action when the event occurs.

When you create a script which inherits from NetworkBehaviour, you can write your own implementation of what should happen when these events occur. To do this, you override the virtual methods on the NetworkBehaviour class with your own implementation of what should happen when the given event occurs.

This is a full list of virtual methods (callbacks) that you can implement on NetworkBehaviour, and where they are called

Server Only

  • OnStartServer

    • called when behaviour is spawned on server

  • OnStopServer

    • called when behaviour is destroyed or unspawned on server

  • OnSerialize

    • called when behaviour is serialize before it is sent to client, when overriding make sure to call base.OnSerialize

Client only

  • OnStartClient

    • called when behaviour is spawned on client

  • OnStartAuthority

    • called when behaviour has authority when it is spawned (eg local player)

    • called when behaviour is given authority by the sever

  • OnStartLocalPlayer

    • called when the behaviour is on the local player object

  • OnStopAuthority

    • called when authority is taken from the object (eg local player is replaced but not destroyed)

  • OnStopClient

    • called when object is destroyed on client by the ObjectDestroyMessage or ObjectHideMessage messages

Example flows

Below is some example call order for different modes

NOTE: Start is called by unity before the first frame, while normally this happens after Mirror's callbacks. But if you dont call NetworkServer.Spawn the same frame as instantiate then start may be called first

Note: OnRebuildObservers and OnSetHostVisibility is now on NetworkVisibility instead of NetworkBehaviour

Server mode

When a NetworkServer.Spawn is called (eg when new client connections and a player is created)

  • OnStartServer

  • OnRebuildObservers

  • Start

Client mode

When local player is spawned for client

  • OnStartAuthority

  • OnStartClient

  • OnStartLocalPlayer

  • Start

Host mode

These are only called on the Player Game Objects when a client connects:

  • OnStartServer

  • OnRebuildObservers

  • OnStartAuthority

  • OnStartClient

  • OnSetHostVisibility

  • OnStartLocalPlayer

  • Start

NetworkBehaviour