🐾PurrNet Docs
Core

NetworkIdentity

NetworkIdentity is the core component that registers a GameObject with the network. Every networked object must have at least one.

What it does

NetworkIdentity assigns a unique network ID to a GameObject and tracks which player owns it, who can observe it, and whether it has been spawned on the network. Without a NetworkIdentity, PurrNet has no way to identify or synchronize that object.

In practice, most scripts inherit from NetworkBehaviour (which extends NetworkIdentity) rather than adding NetworkIdentity as a separate component. Both approaches work.

NetworkIdentity vs NetworkBehaviour

UseWhen
NetworkIdentityYou need a networked object but no script logic (e.g. a pure geometry object tracked over the network).
NetworkBehaviourYou need RPCs, SyncVars, or lifecycle callbacks. Inherit from this instead — it already includes NetworkIdentity.

Lifecycle callbacks

Override these in any NetworkBehaviour to react to network events:

MyNetworkObject.cs
using PurrNet;
using UnityEngine;

public class MyNetworkObject : NetworkBehaviour
{
    protected override void OnSpawned(bool asServer)
    {
        // Called when this object becomes live on the network.
        // asServer=true when running on the server instance.
        if (isOwner) SetupLocalPlayer();
        if (asServer) InitServerState();
    }

    protected override void OnDespawned()
    {
        // Called before this object is removed from the network.
        // Clean up subscriptions, pooled resources, etc.
        CleanupResources();
    }

    protected override void OnOwnerChanged(PlayerID? oldOwner, PlayerID? newOwner)
    {
        // Fires on ALL clients when ownership changes.
        bool iAmNewOwner = newOwner.HasValue && newOwner == localPlayer;
        inputController.enabled = iAmNewOwner;
    }

    protected override void OnOwnerDisconnected()
    {
        // Fires on the server when the owner's connection drops.
        if (isServer)
        {
            // Decide: re-assign, destroy, or make server-controlled?
            GiveOwnership(null); // server takes control
        }
    }

    protected override void OnPoolReset()
    {
        // Called when this object is returned to an object pool.
        // Reset to initial state for reuse.
        _health = maxHealth;
        _ammo = maxAmmo;
        gameObject.SetActive(true);
    }
}

asServer vs isServer

OnSpawned(bool asServer) — the parameter describes the context of that specific callback invocation. isServer — the property tells you whether this machine is the server at any point in time. They are equal in value, but asServer is only available inside OnSpawned.

Key properties

  • networkId — unique identifier assigned by the server at spawn time
  • isServer — true when running on the server
  • isClient — true when running on a client
  • isOwner — true on the client that owns this object
  • IsController — true if you are the owner, or (server when there is no owner)
  • owner — nullable PlayerID? of the current owner
  • isSpawned — true once this object is live on the network
  • localPlayer — shortcut to the local machine's PlayerID