PlayerIdentity
PlayerIdentity<T> is a typed base class for player prefabs that auto-registers each instance in a global static registry. Iterate all players, look up any player by ID, or grab the local player — all without a custom manager.
// PlayerIdentity<T> auto-registers every spawned player.
// Access the typed registry from any script — no manager needed.
// All connected players
foreach (OnlinePlayer p in OnlinePlayer.allPlayers)
{
Debug.Log($"{p.charName} — {p.playerID}");
}
// Count
int count = OnlinePlayer.allPlayers.Count;Overview
Subclass PlayerIdentity<T> where T is your own player class. When the server spawns a player prefab (typically via NetworkManager.SpawnPlayer()), the instance is automatically added to the static Player.allPlayers list. When the player disconnects and the object is despawned, it is removed automatically.
Because the registry is generic and typed, Player.allPlayers returns your exact subclass — no casting, no null-checks.
API reference
| Parameter | Type | Default | Description |
|---|---|---|---|
Player.TryGetLocal(out T player) | bool | — | Returns the local player instance if one has been spawned for the calling client. Returns false on the server or before spawn. |
Player.TryGetPlayer(PlayerID id, out T player) | bool | — | Looks up a player by their PlayerID. Useful for targeting a specific connected client. |
Player.allPlayers | IReadOnlyList<T> | — | A read-only list of every currently spawned player instance. Iterated in join order. Updated automatically on connect/disconnect. |
localPlayer | T? | — | Static convenience accessor — returns the local player or null. Equivalent to calling TryGetLocal. |
playerID | PlayerID | — | The network identity of the connection that owns this player object. Available after OnSpawned. |
isLocalPlayer | bool | — | True if this instance belongs to the local client. Equivalent to isOwner for player objects. |
Defining your player class
using PurrNet;
using UnityEngine;
// The generic parameter is your own class — enables the typed static registry
public class Player : PlayerIdentity<Player>
{
private SyncVar<string> _displayName = new("Player", ownerAuth: true);
private SyncVar<int> _kills = new(0);
private SyncVar<int> _deaths = new(0);
public string displayName => _displayName.value;
public int kills => _kills.value;
public int deaths => _deaths.value;
protected override void OnSpawned()
{
// isLocalPlayer == isOwner for player objects
if (isLocalPlayer)
{
_displayName.value = PlayerPrefs.GetString("PlayerName", "Unknown");
}
// Broadcast the new joiner to all clients
if (isServer) RpcAnnounceJoin(_displayName.value);
}
[ObserversRpc(runLocally: true)]
private void RpcAnnounceJoin(string name)
{
ChatSystem.PostSystemMessage($"{name} joined the game.");
}
// Called by server-side damage systems
public void Kill()
{
if (!isServer) return;
_deaths.value++;
RpcOnKilled();
}
public void AddKill()
{
if (!isServer) return;
_kills.value++;
}
[ObserversRpc(runLocally: true)]
private void RpcOnKilled()
{
// Play death VFX locally on every client
DeathEffect.Play(transform.position);
}
}Getting the local player
From any script, retrieve the local player without a manager singleton:
using PurrNet;
using UnityEngine;
public class HUDController : MonoBehaviour
{
private void Start()
{
// Option A: static accessor (returns null if not yet spawned)
if (Player.localPlayer is { } me)
{
me._displayName.onChanged += name => nameLabel.text = name;
}
}
private void Update()
{
// Option B: TryGetLocal for a safe null-check pattern
if (Player.TryGetLocal(out Player me))
{
killCountLabel.text = me.kills.ToString();
}
}
}Iterating all players — lobby roster
Player.allPlayers is available on every peer (server and clients). Use it to build a lobby list, a score table, or any system that needs to know about every connected player.
using PurrNet;
using UnityEngine;
public class LobbyUI : MonoBehaviour
{
[SerializeField] private PlayerRowUI _rowPrefab;
[SerializeField] private Transform _listParent;
private void OnEnable()
{
// Rebuild list whenever a player joins or leaves
NetworkManager.onPlayerJoined += _ => RebuildList();
NetworkManager.onPlayerLeft += _ => RebuildList();
RebuildList();
}
private void OnDisable()
{
NetworkManager.onPlayerJoined -= _ => RebuildList();
NetworkManager.onPlayerLeft -= _ => RebuildList();
}
private void RebuildList()
{
// Clear old rows
foreach (Transform child in _listParent)
Destroy(child.gameObject);
// allPlayers is sorted by join order and already typed
foreach (Player p in Player.allPlayers)
{
var row = Instantiate(_rowPrefab, _listParent);
row.SetData(
name: p.displayName,
kills: p.kills,
deaths: p.deaths,
isYou: p.isLocalPlayer
);
}
}
}
// On the server — look up a specific player by connection ID
public class AntiCheatSystem : NetworkBehaviour
{
[Server]
public void KickPlayer(PlayerID targetId)
{
if (!Player.TryGetPlayer(targetId, out Player target)) return;
networkManager.Kick(target.playerID, "Cheating detected");
}
}Recommended pattern for player prefabs
PlayerIdentity<T> for your player prefab instead of a plain NetworkBehaviour. The auto-registry eliminates the boilerplate of maintaining a manual player list and ensures the list is always accurate even through host migration or mid-game joins.PlayerIdentity and ownership
isOwner and isLocalPlayer are synonymous on player objects. Only the server can change ownership via GiveOwnership().