Network Variables
Network variables (also known as synced variables) allow Udon scripts to share data across all players in a VRChat instance. Unlike network events, which trigger once and do not persist, network variables ensure that all players—including late joiners—see the correct state of an object.
This guide covers how network variables work, when to use them, and best practices for keeping your networking efficient.
How Synced Variables Work
- Udon Graph
- UdonSharp
VRChat synchronizes variables if you tick the "synced" checkox.
VRChat synchronizes variables marked with the [UdonSynced]
attribute.
[UdonSynced] private string score;
Synced variables behave differently than other variables:
- If your UdonBehaviour uses manual sync, it must call
RequestSerialization()
to synchronize the variable from the owner to all players. - If your UdonBehaviour uses continuous sync, variables will update for all players automatically.
- Your script must perform an ownership transfer to allow another player to modify synced variables.
- Late joiners receive the latest state of the variable, just like other users in the instance. This works regardless of sync type, you do not need to manually call
RequestSerialization
when a user joins. - Not all types of variables can be synced. For example, you cannot sync references to scene objects. Supported types are listed here.
Types of Variable Syncing
There are two types of syncing available:
1. Continuous Sync
- Updates automatically when the owner changes the value.
- Best for frequently updated values (e.g., a progress bar, a player position tracker).
- Can apply interpolation to smooth changes between updates.
- Does not require calling
RequestSerialization()
- updates will be sent out regularly.
2. Manual Sync
- Requires calling
RequestSerialization()
to send data updates. - Best for crucial values that do not change often (e.g., a scoreboard, a game state variable).
- Helps reduce unnecessary network traffic.
Setting the Sync Mode
You can use the Synchronization dropdown in the inspector for an UdonBehaviour to set its sync mode, as shown in the image above.
For UdonSharpBehaviours, you can alternatively use the UdonBehaviourSyncMode attribute to control this from a script, as shown below.
[UdonBehaviourSyncMode(BehaviourSyncMode.Manual)]
public class Example : UdonSharpBehaviour
{
// This class's sync mode is manual.
}
Example: Using Manual Sync
[SerializeField, UdonSynced] private bool isDoorOpen;
public void ToggleDoor()
{
isDoorOpen = !isDoorOpen;
RequestSerialization(); // Manually send update to all players
}
Handling Late Joiners with Variables
Make sure to learn about how to sync variables for late joiners!