unity-multiplayer
Skill32 starsChanged 58 days agoMITView on GitHub
Unity 6 multiplayer and networking guide. Use when building multiplayer games, working with Netcode for GameObjects, NetworkManager, NetworkObject, NetworkBehaviour, RPCs (ServerRpc, ClientRpc), NetworkVariables, or Unity multiplayer services (Relay, Lobby). Covers client-server architecture, state synchronization, and scene management. Based on Unity 6.3 LTS documentation.
1 other project uses a version of this.
Adopted by
Most recently taken up by
- poorvith-mp/skills-gamedevtheir own version, first seen Aug 19, 2026
Other versions1 project uses a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 1
- poorvith-mp/skills-gamedev★ 4same name
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository34 other files it gives its agents.
- unity-2dSkill
- unity-3d-mathSkill
- unity-ai-navigationSkill
- unity-animationSkill
- unity-async-patternsSkill
- unity-audioSkill
- unity-cinemachineSkill
- unity-data-drivenSkill
- unity-ecs-dotsSkill
- unity-editor-toolsSkill
- unity-foundationsSkill
- unity-game-architectureSkill
- unity-game-loopSkill
- unity-graphicsSkill
- unity-input-correctnessSkill
- unity-inputSkill
- unity-level-designSkill
- unity-lifecycleSkill
- unity-lighting-vfxSkill
- unity-npc-behaviorSkill
- unity-packages-servicesSkill
- unity-performanceSkill
- unity-physics-queriesSkill
- unity-physicsSkill
- unity-platformsSkill
- unity-procedural-genSkill
- unity-save-systemSkill
- unity-scene-assetsSkill
- unity-scriptingSkill
- unity-state-machinesSkill
- unity-testingSkill
- unity-ui-patternsSkill
- unity-uiSkill
- unity-xrSkill
The file
We split this skill into the rules it gives an agent
7 rules. About 0 are common, 3 are distinctive, and 4 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈3
Distinctive · estimate
This author's own choices. Worth a look.
4
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 29
Use the Multiplayer Center (Window > Multiplayer > Multiplayer Center) to get package recommendations based on your game's needs.
- Very specific: line 37
Add a NetworkManager component to a GameObject in your scene.
- Very specific: line 80
Every networked GameObject needs a NetworkObject component.
- Very specific: line 119
All networked scripts inherit from NetworkBehaviour instead of MonoBehaviour.
- Very specific: line 299
Use ConnectionApprovalCallback on NetworkManager to validate connecting clients.
- Distinctive: line 364
Important: Do NOT start new scene events within scene event callbacks.
- Distinctive: line 373
Host flow: Create allocation, get join code, configure transport, start host.
≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Unity Multiplayer & Networking
Multiplayer Architecture Overview
Unity's multiplayer ecosystem comprises several layers:
| Layer | Package/Service | Purpose |
| High-Level | Netcode for GameObjects | GameObject-based networking logic |
| High-Level | Netcode for Entities | DOTS-based networking |
| Low-Level | Unity Transport (com.unity.transport) | UDP/WebSocket communication with optional reliability, ordering, fragmentation |
| Services | Relay | NAT traversal via cloud relay servers |
| Services | Lobby | Matchmaking and session discovery |
| Services | Sessions SDK | Player group management |
| Tools | Multiplayer Play Mode | Simulate up to 4 players in-editor |
| Tools | Multiplayer Tools | Analysis, debugging, testing utilities |
Topology options:
- Client-Server (Dedicated): Server has authority; clients send inputs, server validates
- Client-Server (Listen/Host): One player acts as both server and client
- Distributed Authority: Ownership-based authority distributed among clients
Use the Multiplayer Center (Window > Multiplayer > Multiplayer Center) to get package recommendations based on your game's needs.
Netcode for GameObjects Setup
Installation
Install com.unity.netcode.gameobjects (v2.10+) via Package Manager. This automatically pulls in com.unity.transport.
NetworkManager Configuration
Add a NetworkManager component to a GameObject in your scene. It is the singleton entry point for all networking.
using Unity.Netcode;
public class GameLauncher : MonoBehaviour
{
public void StartAsHost()
{
NetworkManager.Singleton.StartHost();
}
public void StartAsServer()
{
NetworkManager.Singleton.StartServer();
}
public void StartAsClient()
{
NetworkManager.Singleton.StartClient();
}
public void Shutdown()
{
NetworkManager.Singleton.Shutdown();
}
}Key NetworkManager properties:
IsServer,IsClient,IsHost-- execution contextConnectedClients-- dictionary of connected clientsConnectedClientsIds-- read-only list of client IDsLocalClientId-- local client's IDSceneManager--NetworkSceneManagerinstanceSpawnManager--NetworkSpawnManagerinstanceNetworkConfig-- project network configuration
Key NetworkManager events: OnClientConnectedCallback, OnClientDisconnectCallback, OnConnectionEvent, OnServerStarted/OnServerStopped, OnClientStarted/OnClientStopped, OnTransportFailure
NetworkObject and NetworkBehaviour
NetworkObject
Every networked GameObject needs a NetworkObject component. It provides identity, ownership, and visibility.
Key properties:
NetworkObjectId(ulong) -- unique ID synchronized across networkIsSpawned-- whether spawned on the networkOwnerClientId-- client ID of current ownerIsOwner-- true if local player owns this objectHasAuthority-- true if local instance has authority
Spawning (server-side only):
// Basic spawn
NetworkObject netObj = Instantiate(prefab).GetComponent<NetworkObject>();
netObj.Spawn();
// Spawn with specific owner
netObj.SpawnWithOwnership(clientId);
// Spawn as player object
netObj.SpawnAsPlayerObject(clientId);
// Despawn
netObj.Despawn(destroy: true);Ownership (server-side only):
netObj.ChangeOwnership(newClientId);
netObj.RemoveOwnership();Visibility:
netObj.NetworkShow(clientId);
netObj.NetworkHide(clientId);
bool visible = netObj.IsNetworkVisibleTo(clientId);NetworkBehaviour
All networked scripts inherit from NetworkBehaviour instead of MonoBehaviour.
Lifecycle methods (in order):
OnNetworkPreSpawn(ref NetworkManager)-- before any spawningOnNetworkSpawn()-- after NetworkObject spawns; register handlers hereOnNetworkPostSpawn()-- after all sibling NetworkBehaviours spawnOnNetworkPreDespawn()-- before despawnOnNetworkDespawn()-- on despawn
Ownership callbacks:
OnGainedOwnership()/OnLostOwnership()OnOwnershipChanged(ulong previous, ulong current)-- fires on all clients
using Unity.Netcode;
public class PlayerController : NetworkBehaviour
{
public override void OnNetworkSpawn()
{
if (IsOwner)
{
// Initialize local player controls
EnableInput();
}
}
void Update()
{
if (!IsOwner) return;
// Only the owner processes input
HandleMovement();
}
public override void OnNetworkDespawn()
{
// Cleanup
}
}Status checks: IsServer, IsClient, IsHost, IsOwner, IsSpawned, IsLocalPlayer, HasAuthority
NetworkVariables
NetworkVariable<T> synchronizes state from server to all clients automatically. Type T must be unmanaged (primitives, unmanaged structs).
public class PlayerHealth : NetworkBehaviour
{
public NetworkVariable<int> Health = new NetworkVariable<int>(
value: 100,
readPerm: NetworkVariableReadPermission.Everyone,
writePerm: NetworkVariableWritePermission.Server
);
public override void OnNetworkSpawn()
{
Health.OnValueChanged += OnHealthChanged;
}
public override void OnNetworkDespawn()
{
Health.OnValueChanged -= OnHealthChanged;
}
private void OnHealthChanged(int oldValue, int newValue)
{
Debug.Log($"Health changed: {oldValue} -> {newValue}");
UpdateHealthUI(newValue);
}
// Server-side only (due to WritePerm.Server)
public void TakeDamage(int amount)
{
if (!IsServer) return;
Health.Value -= amount;
}
}Write permissions:
NetworkVariableWritePermission.Server(default) -- only server can writeNetworkVariableWritePermission.Owner-- only owner can write
NetworkList<T> -- synchronized list (T must be unmanaged + IEquatable<T>):
public class Inventory : NetworkBehaviour
{
public NetworkList<int> Items;
void Awake()
{
Items = new NetworkList<int>();
}
public override void OnNetworkSpawn()
{
Items.OnListChanged += OnItemsChanged;
}
private void OnItemsChanged(NetworkListEvent<int> changeEvent)
{
Debug.Log($"List changed: {changeEvent.Type}");
}
}RPCs (ServerRpc, ClientRpc)
RPCs are remote procedure calls between server and clients. Methods must be in a NetworkBehaviour and use the [Rpc] attribute with a SendTo target.
Unified Rpc Attribute (v2.x)
public class CombatSystem : NetworkBehaviour
{
// Server executes this when any client calls it
[Rpc(SendTo.Server)]
void AttackRpc(int targetId, RpcParams rpcParams = default)
{
ulong senderId = rpcParams.Receive.SenderClientId;
ProcessAttack(senderId, targetId);
}
// All clients (and host) execute this
[Rpc(SendTo.ClientsAndHost)]
void ShowDamageEffectRpc(Vector3 position, int damage)
{
SpawnDamagePopup(position, damage);
}
// Only the owner executes this
[Rpc(SendTo.Owner)]
void NotifyOwnerRpc(string message)
{
Debug.Log(message);
}
// Everyone including sender
[Rpc(SendTo.Everyone)]
void PlaySoundRpc(int soundId)
{
AudioManager.Play(soundId);
}
}SendTo Targets
| Target | Description |
Server | Executes on server; locally if called on server |
NotServer | All clients except server (excludes host) |
Owner | Object's owner only |
NotOwner | Everyone except owner |
Authority | Server in client-server; owner in distributed authority |
NotAuthority | All non-authority instances |
ClientsAndHost | All clients including host |
Everyone | All instances on the observer list |
Me | Local execution only |
NotMe | Everyone except sender |
SpecifiedInParams | Target set at runtime via RpcSendParams |
Legacy Attributes (still supported)
[ServerRpc]
void RequestSpawnServerRpc(ServerRpcParams rpcParams = default)
{
// Runs on server; only owner can call by default
}
[ClientRpc]
void UpdateUIClientRpc(int score)
{
// Runs on all clients
}Connection Approval
Use ConnectionApprovalCallback on NetworkManager to validate connecting clients.
// Server-side: register approval callback
NetworkManager.Singleton.ConnectionApprovalCallback = (request, response) =>
{
string password = System.Text.Encoding.UTF8.GetString(request.Payload);
response.Approved = (password == "secret");
response.CreatePlayerObject = response.Approved;
// Optionally set: response.PlayerPrefabHash, response.Position, response.Rotation
if (!response.Approved) response.Reason = "Invalid password";
response.Pending = false; // Signal decision is made
};
// Client-side: set payload before connecting
NetworkManager.Singleton.NetworkConfig.ConnectionData =
System.Text.Encoding.UTF8.GetBytes("secret");
NetworkManager.Singleton.StartClient();Scene Management
NetworkSceneManager (accessed via NetworkManager.Singleton.SceneManager) handles synchronized scene loading.
public class GameSceneManager : NetworkBehaviour
{
public void LoadGameScene()
{
if (!IsServer) return;
// Server-only: loads scene on all clients
NetworkManager.Singleton.SceneManager.LoadScene("GameScene", LoadSceneMode.Single);
}
public void LoadAdditiveScene()
{
if (!IsServer) return;
NetworkManager.Singleton.SceneManager.LoadScene("Arena", LoadSceneMode.Additive);
}
public override void OnNetworkSpawn()
{
NetworkManager.Singleton.SceneManager.OnSceneEvent += OnSceneEvent;
}
void OnSceneEvent(SceneEvent sceneEvent)
{
switch (sceneEvent.SceneEventType)
{
case SceneEventType.LoadComplete:
Debug.Log($"Client {sceneEvent.ClientId} loaded {sceneEvent.SceneName}");
break;
case SceneEventType.LoadEventCompleted:
Debug.Log($"All clients loaded {sceneEvent.SceneName}");
break;
case SceneEventType.SynchronizeComplete:
Debug.Log($"Client {sceneEvent.ClientId} fully synchronized");
break;
}
}
}Scene event types: OnLoad, OnUnload, OnSynchronize, OnLoadComplete, OnUnloadComplete, OnLoadEventCompleted, OnUnloadEventCompleted, OnSynchronizeComplete
Important: Do NOT start new scene events within scene event callbacks.
Unity Multiplayer Services (Relay, Lobby)
Full integration examples in
references/transport-layer.md
Unity Relay
Relay provides NAT punchthrough via cloud relay servers (no port forwarding needed). Requires UGS authentication.
Host flow: Create allocation, get join code, configure transport, start host. Client flow: Join allocation with code, configure transport, start client.
// Host: create relay and start
Allocation allocation = await RelayService.Instance.CreateAllocationAsync(maxPlayers);
string joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
transport.SetRelayServerData(allocation.ToRelayServerData("dtls")); // "dtls" = encrypted UDP
NetworkManager.Singleton.StartHost();
// Client: join relay and connect
JoinAllocation join = await RelayService.Instance.JoinAllocationAsync(joinCode);
transport.SetRelayServerData(join.ToRelayServerData("dtls"));
NetworkManager.Singleton.StartClient();Unity Lobby
Lobby provides session discovery and matchmaking. Store the Relay join code in lobby data.
// Create lobby with relay join code
_lobby = await LobbyService.Instance.CreateLobbyAsync(name, maxPlayers, new CreateLobbyOptions {
Data = new Dictionary<string, DataObject> {
{ "JoinCode", new DataObject(DataObject.VisibilityOptions.Member, relayJoinCode) }
}
});
// Query available lobbies
QueryResponse response = await Lobbies.Instance.QueryLobbiesAsync(new QueryLobbiesOptions {
Filters = new List<QueryFilter> {
new QueryFilter(QueryFilter.FieldOptions.AvailableSlots, "0", QueryFilter.OpOptions.GT)
}
});
// Join and extract relay code
Lobby lobby = await LobbyService.Instance.JoinLobbyByIdAsync(lobbyId);
string joinCode = lobby.Data["JoinCode"].Value;
// IMPORTANT: Send heartbeats every 15s or lobby expires
await LobbyService.Instance.SendHeartbeatPingAsync(lobby.Id);Common Patterns
Player Spawning with Custom Prefab (Server-Side)
// In a NetworkBehaviour on the server:
NetworkManager.Singleton.OnClientConnectedCallback += (ulong clientId) => {
GameObject player = Instantiate(playerPrefab);
player.GetComponent<NetworkObject>().SpawnAsPlayerObject(clientId);
};Owner-Authoritative Movement
public class PlayerMovement : NetworkBehaviour
{
public NetworkVariable<Vector3> Position = new(writePerm: NetworkVariableWritePermission.Owner);
void Update()
{
if (!IsOwner) return;
// Note: Uses legacy Input for brevity. See unity-input for the new Input System.
Vector3 move = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
transform.position += move * Time.deltaTime * 5f;
Position.Value = transform.position;
}
}Server-Authoritative with Input RPCs
public class ServerAuthMovement : NetworkBehaviour
{
[Rpc(SendTo.Server)]
void MoveRpc(Vector3 input) { transform.position += input * Time.deltaTime * 5f; }
void Update()
{
if (!IsOwner) return;
MoveRpc(new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"))); // legacy Input; see unity-input
}
}Anti-Patterns
- Writing to NetworkVariable without authority -- Only the server (or owner with
WritePerm.Owner) can write. Client writes are silently ignored. - Forgetting
IsOwnerchecks in Update -- Without owner checks, all clients run input logic, causing conflicting state. - Using
InstantiatewithoutSpawn-- Objects created withInstantiatealone are local-only. Always callSpawn()on theNetworkObjectfor network visibility. - Spawning from client code -- Only the server can spawn NetworkObjects. Clients must send an RPC to request spawning.
- Heavy data in RPCs instead of NetworkVariables -- RPCs are fire-and-forget; late joiners miss them. Use NetworkVariables for persistent state.
- Not unsubscribing from OnValueChanged -- Subscribe in
OnNetworkSpawn, unsubscribe inOnNetworkDespawnto prevent leaks. - Starting scene events inside scene event callbacks --
NetworkSceneManagerforbids this; causes undefined behavior. - Not sending Lobby heartbeats -- Lobbies expire without periodic
SendHeartbeatPingAsynccalls (every 15-30 seconds). - Using
NetworkVariablefor frequent small updates -- For high-frequency data (position), preferNetworkTransformor custom serialization. - Calling RPCs before
OnNetworkSpawn-- RPCs require the NetworkObject to be spawned. Defer toOnNetworkSpawn.
Key API Quick Reference
| Class | Key Members |
NetworkManager | StartHost(), StartServer(), StartClient(), Shutdown(), Singleton, ConnectedClients, LocalClientId, SceneManager, ConnectionApprovalCallback |
NetworkObject | Spawn(), SpawnWithOwnership(), SpawnAsPlayerObject(), Despawn(), ChangeOwnership(), NetworkShow(), NetworkHide(), NetworkObjectId, OwnerClientId, IsOwner, HasAuthority |
NetworkBehaviour | OnNetworkSpawn(), OnNetworkDespawn(), OnGainedOwnership(), OnLostOwnership(), IsServer, IsClient, IsHost, IsOwner, IsSpawned, HasAuthority, RpcTarget |
NetworkVariable<T> | .Value, OnValueChanged, ReadPerm, WritePerm, CheckDirtyState() |
NetworkList<T> | Add(), Remove(), Insert(), Clear(), Count, OnListChanged |
NetworkSceneManager | LoadScene(), UnloadScene(), OnSceneEvent |
[Rpc(SendTo.X)] | Server, Owner, NotOwner, ClientsAndHost, Everyone, NotMe, Authority, SpecifiedInParams |
Related Skills
- unity-foundations -- Core Unity concepts, GameObjects, components, scene hierarchy
- unity-scripting -- C# scripting, MonoBehaviour lifecycle, coroutines
- unity-physics -- Physics systems; use
NetworkRigidbodyfor synced physics
Additional Resources
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
Reviews can't be read right now.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

