๐ Lesson 6.1: Networking Foundations โ Client/Server & Topologies
Multiplayer is a different kind of hard. The code isn't the challenge so much as the physics of the problem: messages take time to arrive, some never do, and players will cheat if you let them. Before writing a line of Netcode, you need the concepts that every design decision hangs on โ topologies, authority, and why "just sync everything" doesn't work. This lesson builds that foundation.
๐ฏ Learning Objectives
By the end of this lesson, you will be able to:
- Explain the core problems networking must solve โ latency, loss, and trust
- Compare peer-to-peer and client-server topologies
- Distinguish a host (listen server) from a dedicated server
- Explain the authoritative-server model and why it prevents cheating
- Set up Netcode for GameObjects and start a host, server, or client
Estimated Time: 60 minutes ยท Prerequisite: Confident C#; Intermediate architecture (events, ScriptableObjects)
In This Lesson
Why It's Hard
A single-player game reads input and updates the world in the same instant. A networked game can't. Three hard facts shape everything:
- Latency. A message from one machine to another takes tens to hundreds of milliseconds. By the time you see another player's position, it's already old.
- Loss & order. Packets get dropped, duplicated, or arrive out of order. You can't assume every message lands, or lands in sequence.
- Trust. You control the server, not the players' machines. Any value a client sends could be forged by a cheater.
Every networking technique โ authority, prediction, interpolation, reliable vs unreliable channels โ exists to cope with one of these. This module focuses on getting a correct, cheat-resistant multiplayer game working; the advanced smoothing techniques (client-side prediction, lag compensation) are a specialisation we point to at the end.
Topologies
A topology is the shape of who-talks-to-whom. Two broad families:
Nearly all modern multiplayer โ and all of this module โ uses client-server. It gives you a single source of truth, a natural place to enforce rules, and a far easier security story. Netcode for GameObjects is a client-server library.
Host vs Dedicated Server
Client-server has two flavours, and Netcode supports both:
๐ฅ๏ธ Dedicated Server
The server is its own process with no player attached โ a headless build running in the cloud. Every player is a pure client. Best for competitive games and larger player counts; costs money to host.
๐ Host (Listen Server)
One player's machine runs the server and a local client at once. Cheap and simple โ perfect for co-op and small sessions โ but the host has a latency advantage and the session dies if they leave.
A host is just "server + client in one process." Everything you write works the same either way, which is why you'll usually develop with a host (one machine, easy to test) and can deploy as a dedicated server later without rewriting your gameplay code.
Netcode for GameObjects
Netcode for GameObjects (NGO) is Unity's official high-level networking library for the GameObject workflow. It handles connection, spawning networked objects, synchronising state, and remote calls, over a pluggable transport (Unity Transport by default). Install it from the Package Manager, then add a NetworkManager component to a scene object โ it's the brain of the whole session.
The NetworkManager holds your transport, the list of spawnable network prefabs, and the connection settings. Its singleton, NetworkManager.Singleton, is how your code starts and queries the session. Around it you'll build with two more types you'll meet next lesson: NetworkObject (marks a GameObject as network-spawnable) and NetworkBehaviour (the base class for networked scripts, replacing MonoBehaviour).
๐ก One codebase, three roles. The same build can run as host, server, or client โ which role it takes is decided at runtime by which "Start" method you call. Your networked scripts checkIsServer/IsClient/IsOwnerto branch behaviour.
Starting a Session
With a NetworkManager in the scene, starting a session is one call. A tiny UI to pick a role is the classic first script:
using Unity.Netcode;
using UnityEngine;
public class ConnectionMenu : MonoBehaviour
{
void OnGUI()
{
// Only show the menu before a session has started.
if (NetworkManager.Singleton.IsClient || NetworkManager.Singleton.IsServer)
return;
if (GUILayout.Button("Host")) NetworkManager.Singleton.StartHost(); // server + client
if (GUILayout.Button("Server")) NetworkManager.Singleton.StartServer(); // dedicated
if (GUILayout.Button("Client")) NetworkManager.Singleton.StartClient(); // join a host/server
}
}
StartHost()โ become the server and a local player. The go-to for testing.StartServer()โ become a headless dedicated server, no local player.StartClient()โ connect to a server at the transport's configured address.
To test locally, build the game, run one instance and click Host, run another and click Client โ the client connects to 127.0.0.1 by default. That two-window loop is how you'll iterate on everything in this module.
Hands-on Challenge
๐๏ธ Exercise 1: Stand up a session
Objective: Get two instances talking.
- Install Netcode for GameObjects from the Package Manager.
- Add an empty GameObject, add a NetworkManager component, and set its transport to Unity Transport.
- Add the
ConnectionMenuscript to a scene object. - Build the project. Run the build and click Host; run a second copy (or use the Editor) and click Client. Confirm in the console that the client connects.
๐ก Hint
The NetworkManager logs connection events. If the client can't connect, check the transport's address/port match and that the host started first. Multiplayer Play Mode (a Unity package) can spin up multiple virtual players in one editor to speed this loop up.
๐๏ธ Exercise 2: Reason about authority
A player's client sends the server "I picked up the gold coin." List two things the server should verify before accepting it, and explain what a cheat-client could do if the server just trusted the message.
โ Answer
The server should verify the coin still exists (not already collected) and that the player is actually close enough to it (position within pickup range). If the server blindly trusted the message, a cheat-client could claim every coin instantly from across the map โ classic exploit. Authority means the server checks the rules before changing shared state.
๐ฏ Quick Quiz
Question 1: Which topology does Netcode for GameObjects use, and why is it preferred?
Question 2: What is a "host" in Netcode?
Question 3: In the authoritative-server model, who may directly change shared game state?
Summary
๐ Key Takeaways
- Networking's hard facts are latency, packet loss/order, and trust โ every technique addresses one of them.
- Client-server (used by NGO) beats peer-to-peer for a single source of truth and security.
- A host is server + local client in one process; a dedicated server has no local player. Your gameplay code works for both.
- The authoritative server owns shared state; clients send validated requests โ this is what stops cheating.
- Netcode for GameObjects centres on a
NetworkManager; start a session withStartHost/StartServer/StartClient.
๐ What's Next?
You can start a session; now you need things in it. In Lesson 6.2: NetworkObjects, Spawning & Ownership we make GameObjects network-aware, spawn player objects, and untangle who owns and controls what.
๐ The foundation is set
Client-server, an authoritative host, and a NetworkManager ready to go. Every multiplayer feature in this module rests on "server writes, clients request" โ keep it in mind and the rest is mechanics.