Course contentsShow
← Unity Game Development
Lesson 780 of 82812. Multiplayer and DeploymentFree lesson

Setting Up the NetworkManager Architecture

Configure the NetworkManager component to establish the core communication bridge between your game server and connected clients.

In this lesson, you will learn how to configure the NetworkManager component to act as the authoritative gateway for your game’s client-server communication.

The Role of the NetworkManager

Think of the NetworkManager as the "traffic controller" of your multiplayer architecture. In an authoritative server model, this component is responsible for managing the lifecycle of the connection, spawning network-aware objects, and maintaining the master state of the game world. Without a properly configured NetworkManager, clients have no roadmap for where to connect or how to synchronize their local view with the server's truth.

Core Configuration Setup

To establish the bridge between your server and clients, you must ensure the NetworkManager is attached to a dedicated GameObject in your scene. This manager needs a Player Prefab assigned to it; this is the authoritative template the server uses to instantiate the player character whenever a client connects successfully.

Beyond the prefab, you must configure the Network Address and Network Port. These act as the digital coordinates for your handshake. For a robust architecture, you typically define these settings via a script to ensure they are consistent across your build environments:

public class CustomNetworkManager : NetworkManager
{
    public override void OnServerAddPlayer(NetworkConnectionToClient conn)
    {
        // Custom logic: Instantiate player at a valid spawn point
        GameObject player = Instantiate(playerPrefab, startPosition, Quaternion.identity);
        NetworkServer.AddPlayerForConnection(conn, player);
    }
}

Bridging the Gap

When a client triggers a connection, the NetworkManager initiates the handshake. It validates the connection request, and upon approval, it orchestrates the transfer of the current world state. By centralizing this logic within the NetworkManager, you ensure that every player enters the game world under the same set of rules, adhering to the authoritative server logic you've previously defined. If you try to manage connection handshakes in disparate scripts, you will inevitably face race conditions during scene transitions or player spawning.

Key Takeaway: The NetworkManager is your architectural foundation; it must be configured to consistently handle connection handshakes and object spawning to ensure the server remains the single source of truth for all connected clients.