Netcode for GameObjects Integration for Multiplayer (Unity)
Netcode for GameObjects (NGO)—official Unity multiplayer solution, successor to UNET. Unlike Mirror, NGO deeply integrates with Unity Gaming Services: works with Relay, Lobby, Matchmaker out-of-box. For mobile projects, this means fast startup without server infrastructure setup—but lock-in to Unity ecosystem.
NetworkManager and Bootstrap
NGO requires NetworkManager with configured transport. For mobile, recommend UnityTransport with RelayServerData—Unity Relay proxies UDP traffic through their servers, bypassing NAT without dedicated server:
async Task StartHost()
{
var allocation = await RelayService.Instance.CreateAllocationAsync(maxPlayers: 4);
var joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
var relayServerData = new RelayServerData(allocation, "udp");
NetworkManager.Singleton.GetComponent<UnityTransport>()
.SetRelayServerData(relayServerData);
NetworkManager.Singleton.StartHost();
// Share joinCode to Lobby for other players
}
joinCode—6-character string clients use to connect via JoinAllocationAsync. Replaces need for open IP for P2P or dedicated server for small lobbies.
NetworkVariable and State Sync
NetworkVariable<T>—typed variable with automatic sync:
public class PlayerState : NetworkBehaviour
{
private NetworkVariable<int> _health = new NetworkVariable<int>(
100,
NetworkVariableReadPermission.Everyone,
NetworkVariableWritePermission.Server
);
public override void OnNetworkSpawn()
{
_health.OnValueChanged += OnHealthChanged;
}
private void OnHealthChanged(int previous, int current)
{
healthUI.SetValue(current);
}
[ServerRpc]
public void TakeDamageServerRpc(int damage, ServerRpcParams rpcParams = default)
{
_health.Value = Mathf.Max(0, _health.Value - damage);
}
}
NetworkVariableWritePermission.Server—only server writes. NetworkVariableWritePermission.Owner—only owner. Client-side prediction: Owner writes locally, but server is authoritative and can rollback.
NetworkBehaviour and Ownership
NGO strictly separates: IsServer, IsClient, IsHost, IsOwner. IsOwner = true for object this client manages. Typical mistake—not checking IsOwner in Update, then all clients process input for all players.
Ownership transfer: networkObject.ChangeOwnership(clientId)—useful for pickable objects (pick up—become owner).
Boss Room as Reference
Unity published Boss Room—sample NGO project with full coop for mobile. Implements: action-animation sync, anticipation (client plays animation before server confirmation), client-authoritative movement with server correction. Best source for NGO patterns—read before coding.
Lobby and Matchmaking
var options = new CreateLobbyOptions {
IsPrivate = false,
Data = new Dictionary<string, DataObject> {
{ "GameMode", new DataObject(DataObject.VisibilityOptions.Public, "deathmatch") },
{ "Map", new DataObject(DataObject.VisibilityOptions.Public, "forest") }
}
};
var lobby = await LobbyService.Instance.CreateLobbyAsync("My Lobby", 4, options);
Lobby SDK stores metadata, filtering via QueryFilter. Heartbeat via LobbyService.Instance.SendHeartbeatPingAsync—without it, lobby deletes after 30 sec.
Mobile Nuances
Background. NetworkManager.Singleton.Shutdown() on OnApplicationPause(true) on iOS—else Relay connection hangs and doesn't recover on return. On OnApplicationPause(false)—StartClient() again with saved joinCode.
Batching. NGO batches messages by default. NetworkConfig.NetworkTickSystem sets tick frequency (30-60 Hz). For turn-based, lower to 10 Hz—fewer batches, less traffic.
Versioning. NetworkConfig.NetworkTickSystem + NetworkConfig.ProtocolVersion—on app update, old clients don't connect to new servers. On mobile, check client version before session start.
Timeline
Basic NGO integration with Relay, Lobby, NetworkVariable for 2-4 players: 1-2 weeks. Full system with client prediction, matchmaking, mobile optimization: 1.5-2.5 months. Cost calculated individually.







