To use Network Variable in code they must bound a type NetworkVariable<T> where T must be included into allowed types.
private NetworkVariable<int> variable = 0;
Once defined you can use it as if any other script variable
this.variable += 1;
int variableValue = this.variable;
This provides greater control and access to various callbacks you can leverage. Below are some examples:
Detecting Changes
You can detect when the object was updated on the passive instance to apply changes only when occur.
this.variable.OnValueChange((int oldValue, int newValue) => {
Debug.Log($"Value was updated from {oldValue} to {newValue} ");
});
Note: This will only be triggered on passive object
Important: Modifying the value of a NetworkVariabledirectly on a passive object can disrupt its internal synchronization logic.
This may prevent the OnValueChanged callback from being triggered in the future—even when a new value is received from the active owner. *unless you used variable.SetValue()
Synchronizing local variables
ObjectNet allows to synchronization of network variables with local variables automatically. The following script allows you to keep a local variable synchronized with a network variable, no matter where you change the value, both variables will have the same value.
NetworkVariable<string> variable = "Hello world";
string localVariable = "Start text";
this.variable.OnSynchonize(() => { return this.localVariable; },
(string value) => { this.localVariable = value; });
// In this case variable Network will also receive the new value "New value from local"
this.localVariable = "New value from local";
// In this case local variable will also receive the new value "New value from Network Variable"
this.variable = "New value from Network variable";
Important: Never modify the value of a networked variable on a passive object, especially if it uses Reliable DeliveryMode. This can interfere with the value sent from the active source and cause players to see different results (desync).
Variables DeliveryModes:
there are two Modes NetworkVariables uses Reliable and UnreliableDelivery Mode.
Reliable variables send data only when it detects changes or when a new client join, This makes them ideal for data that changes infrequently, as they reduce bandwidth usage and ensure consistency.
Avoid using Reliable mode for variables that change frequently (e.g., more than once per second), as it may overwhelm the network and introduce latency.
Examples of Reliable variables:
Health, Player name / username, Score / Points, Kill / death count (K/D), Game state (Win/Loss, Start Match, Pause), Quest or mission updates, Timers, lobby settings, Inventory items, map selection, Skins selection, Environmenatl states (e.g., doors open) , etc...
By defalue all NetworkVaraibles will be using Reliable DelivaryMode
Unreliable variables are sent At a constant rate through the network object’s update loop, Using a lightweight, unreliable buffer, making it fast for frequently changing variables (more than once per second constantly).
Unreliable variable could arrive out of order or not at all, making it bad for timers and critical game events
Examples of Unreliable variables:
Input, Position , Target Position, Rotation, Animation parameters, Aim direction / camera look
Change Delievry Mode from Prefab Database
You can change the Network Variable Delivary Mode from the Network Manager Prefab DataBase as shown in the second Image
Change Delievry Mode in code
You can change the Network variable Delivary Mode from Reliable to Unreliable through code
Note: You should only change Delivary Mode in Awake() changing it mid game may cause unexpected behavoiur
Changes Detection Mode (In Reliable Variables)
🛠️ Automatic Detection Mode
By default, all Reliable Variablesin ObjectNet use Automatic Detection Mode. In this mode, ObjectNet continuously monitors variable changes and efficiently packs all detected changes into a single buffer, reducing bandwidth usage and improving performance.
The detection rate is tied to the Object send rate, ensuring that:
Only variables with actual changes are included in the update buffer.
If no changes occur, no network packets are sent, optimizing traffic flow.
🛠️ Manual Detection Mode
When you mark a variable as Manual Detection, ObjectNet will not sync it automatically. The developer take full responsibility for sending these reliable variables by themself.
To send all Manual Reliable variables across all objects use:
This method does not immediately send variable data. It simply Invalidate all variables, allowing them to be detected and included in the next regular send cycle.
📝 Note:
If you are using you can force an individual variable to be included in the next synchronization cycle by calling:
variableName.Invalidate();
This manually flags the variable as changed, ensuring it is sent even if its value hasn't been modified.
FAQ:
What if I want to modify a variable from a passive object?
You cannot directly modify a variable on a passive object, as the change will not be synced across other clients.
To update the value correctly, you should send an to the active object and let it handle the modification. This ensures that the update is properly synced for everyone.
NetworkVariable<int> health = 100;
// Anywhere in your code (on active or passive)
NetworkExecute<int>(Damage, 20);
// This ensures the value is only changed by the active instance
private void Damage(int damage){
if (IsActive())
health -= damage;
}
💡 Even passive objects can call NetworkExecute, but the logic inside will only run on the active side — keeping everything in sync.