Skip to main content

Ownership and synced values

Every object with scripts has exactly one owner: at first the room's master. Only the owner can change [Synced] values. Changes are sent to everyone automatically, and the other players' copies get OnDeserialization().

[Synced] private int count;

public void Increment()
{
Networking.SetOwner(Networking.LocalPlayer, gameObject); // 1. become the owner
count++; // 2. change synced values
UpdateLabel(); // 3. update your own view
}

public override void OnDeserialization() { UpdateLabel(); } // everyone else updates here

Ownership​

API
Networking.SetOwner(Networking.LocalPlayer, gameObject)Take ownership. You can change synced values straight away.
Networking.IsOwner(gameObject)You own it
Networking.GetOwner(gameObject)Its owner
Networking.IsMasterYou are the room's master
OnOwnershipTransferred(player)Runs on everyone when the owner changes
  • When a player leaves, the master takes over their objects. When the master leaves, another player becomes master.
  • If two players take ownership at the same moment, one wins. The other's changes are undone and it gets OnDeserialization() with the real values.
  • A non-owner's writes to [Synced] fields are ignored (with one warning).

What can be synced​

TypeWords
bool, int, float1
double2
Vector2 / Vector3 / Vector42 / 3 / 4
Quaternion, Color4
string (up to 60 UTF-8 bytes)16
fixed-length arrays of the above (not strings), e.g. new int[4]length × element

A script can sync up to 96 words. The compiler tells you if you go over.

Synced arrays​

[Synced] private int[] scores = new int[4]; // the length is fixed by the initializer

scores[slot] += 1; // index it as usual
for (int i = 0; i < scores.Length; i++) total += scores[i];

Use elements, .Length and foreach. To assign, pass or return a whole synced array, copy it in a loop.

Late joiners​

Players who join later receive the current synced values before their scripts' Start. Anything you only keep in normal fields (for example a local animation) has to be derived from synced values.

RequestSerialization()​

StageTime sends changed values automatically, so you never need it. It exists (and does nothing harmful) so code written for manual sync still compiles.