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

Defining Networked Objects with NetworkObject

Implement the NetworkObject component to ensure specific game entities are tracked and synchronized across all connected game instances.

In this lesson, you will learn how to identify specific game entities as networked entities using the NetworkObject component to ensure they remain synchronized across all connected client and server instances.

From Local Entities to Networked Entities

Up until now, we have discussed the high-level architecture of multiplayer games, such as authoritative servers and state synchronization. However, Unity’s networking layer needs a specific signal to distinguish between a "local-only" prop (like a decorative bush) and an "important" object (like a player character or a health pack).

The NetworkObject component acts as that signal. When you attach this component to a GameObject in your hierarchy, you are effectively telling the NetworkManager to track this entity throughout its lifecycle. Once a NetworkObject is present, the engine assigns it a NetworkObjectId. This unique identifier allows the server and all connected clients to refer to the exact same object, even if they exist on different machines.

Implementing NetworkObject

To network an object, you simply add the NetworkObject component to your prefab. Without this, any scripts attached to the object—even those using NetworkBehaviour—will fail to sync because the networking engine does not know that this object exists within the shared global state.

using Unity.Netcode;

public class PlayerController : NetworkBehaviour
{
    // Because this script inherits from NetworkBehaviour, 
    // it requires a NetworkObject component on the same GameObject.
    public override void OnNetworkSpawn()
    {
        if (IsOwner)
        {
            Debug.Log("This entity is officially synchronized and controlled by me!");
        }
    }
}

When a NetworkObject is spawned, it is registered with the NetworkManager. If a new client joins late, the server will "send" the state of all currently active NetworkObject instances to that client so their world mirrors the server's world exactly. Without this component, the object effectively remains invisible to the networking system, rendering any state synchronization logic useless. It is the fundamental "hook" that allows an object to participate in the multiplayer simulation.

Key Takeaway: The NetworkObject component is the mandatory foundation for any entity requiring synchronization, as it assigns a unique identifier that allows the network to track, replicate, and manage that object’s existence across all instances.