Introduction to GhostObjects
Understand how GhostObjects let you build networked multiplayer gameplay with GameObjects and MonoBehaviours.
GhostObjects make a GameObject networked, so that it moves and behaves the same way on every player's screen in real time. You add a GhostObject component to a GameObject, write your gameplay logic in MonoBehaviours, and Netcode synchronizes the object's state between the server and its clients. Netcode is built on Unity's Data-Oriented Technology Stack (DOTS) and takes advantage of its performance.
This page introduces the main building blocks of GhostObjects. It explains how to make a GameObject networked with the GhostObject component, write prediction logic in a class that extends GhostBehaviour, and replicate and predict state with the GhostField type. It also helps you understand server authority and prediction in Netcode, and choose the right networking API.
You work with two main points of access: the static Netcode class for project-wide operations such as starting a session, and the GhostObject component and GhostBehaviour scripts for anything that concerns a single networked object.
Make a GameObject networked
The GhostObject component is the entry point for making a GameObject networked. You add it to a prefab in a similar way to how you add a Rigidbody component to make a GameObject respond to physics. To add it, in the Inspector window, select Add Component > Multiplayer > Ghost Object. A GameObject can have only one GhostObject component, and you place it on the prefab root.
The GhostObject component also holds the configuration that controls how Netcode replicates the object, such as its importance, whether it has an owner, and whether clients predict or interpolate it. The Inspector window groups these settings into the General, Prediction and Interpolation, Optimization, and Advanced sections. Netcode registers GhostObject prefabs automatically so that the server can spawn them and clients can instantiate the matching copies.
Understand what a GhostObject replicates
Adding a GhostObject component doesn't replicate the whole GameObject. It replicates the transform, and you opt in to everything else:
| State | Replicated |
|---|---|
| Position and rotation | Yes, automatically. |
| Scale, including per-axis scale | Yes, automatically. Enable Uniform Scale to replicate a single scale value instead of three. |
| A Rigidbody component | Yes, automatically to clients that predict it. You can disable this to save on bandwidth. |
| The parent ghost | Only when you enable Parent Replication. |
| Your own gameplay state | Use a GhostField or a GhostComponentRef<T> inside a GhostBehaviour. |
| Any other component's fields | No. Netcode doesn't replicate any other MonoBehaviour or component state. |
If a value doesn't arrive on a client, the usual cause is that nothing declares it as replicated state. Limit what you replicate, because replicating more state can increase your bandwidth costs, depending on how you distribute your game.
Write prediction logic
To add networked gameplay logic to a GhostObject component, create a MonoBehaviour that extends the GhostBehaviour class. A GhostBehaviour gives you access to the networked object and provides methods that run at the correct point in the prediction loop:
PredictionUpdateruns your simulation logic on the server and on predicting clients, tick accurate with the prediction loop.PredictionGatherInputcollects player input, tick accurate withPredictionUpdate, so the simulation uses the input you gathered for that tick.PredictionPhysicsUpdateruns your physics logic tick accurate with the physics step. Netcode captures physics stepping so that physics runs as close as possible to the same way on the client and the server.
Because these prediction methods run inside the prediction loop rather than in Unity's standard Update, they participate correctly in prediction and rollback. That also means the loop can run your code several times for the same tick. For the rules that follow from this, refer to Write prediction logic in a GhostBehaviour. For more information about how the prediction loop drives simulation, refer to Prediction.
Replicate and predict state
To replicate a value from the server to clients, declare it as a GhostField in your GhostBehaviour. A GhostField is eventually consistent: the server has authority over its value, and Netcode automatically rolls it back and re-applies it during prediction so that predicting clients stay consistent with the server.
You read and write it like a normal field, and Netcode handles the underlying serialization and synchronization for you.
public GhostField<int> Health;
Because a GhostField is eventually consistent, clients are guaranteed the latest value but not every value it passes through. For one-off events that must arrive exactly once, such as a chat message or a round announcement, use a remote procedure call (RPC) instead. For more information, refer to Send events with RPCs and custom messages.
Understand server authority and prediction
GhostObjects are server authoritative, with client prediction:
- The server has final authority over every networked object. Clients receive snapshots of the server state and present it locally.
- Objects can be interpolated or predicted. Interpolated objects smoothly follow received snapshots, while predicted objects simulate ahead of the server and reconcile when new snapshots arrive.
- Any change a client makes to a predicted object is a prediction that the server can overwrite when authoritative data arrives.
For background on these concepts, refer to Synchronization with ghosts, Interpolation and extrapolation, and Introduction to prediction.
Choose the right networking API
Unity provides more than one way to build networked multiplayer, so it's important to know where GhostObjects fit:
- GhostObjects: Use them when you want the performance of the entity component system (ECS) and the features of this package, such as prediction, relevancy, importance scaling, delta compression, and quantization, but prefer to author and script with GameObjects and MonoBehaviours.
- The entity-based ghost workflow: The lower-level Netcode API. Use it when you work directly with entities and want full control over components, systems, and serialization. It lets you write jobs and Burst-compiled code that affects your gameplay state directly.
GhostComponentRefbridges the gap between GhostObjects and ECS system logic. - Netcode for GameObjects: A separate Unity networking package with its own architecture. It isn't related to GhostObjects, even though both use GameObjects. A bridge API connects Netcode for GameObjects and GhostObjects, so that you can take advantage of some of this package's optimizations.
GhostObjects and the entity-based ghost workflow share the same underlying replication pipeline, so you can combine them in the same project. For more information, refer to Hybrid GhostObjects and entities in the same project.
You can also use Netcode for GameObjects alongside GhostObjects, including a NetworkObject and a GhostObject component on the same GameObject. A NetworkBehaviour's NetworkVariable fields replicate through Netcode for GameObjects rather than through the snapshot system, so each object's state travels through the package that owns it.
For more information about when to use GhostObjects, refer to How and when to use GhostObjects.