SyncVar
SyncVars are the most simple way to automatically synchronize a single variable over the network.
public class YourClass : NetworkBehaviour
{
private readonly SyncVar<float> _health = new SyncVar<float>();
}
/* Any time _health is changed on the server the
* new value will be sent to clients. */private readonly SyncVar<float> _health = new SyncVar<float>(new SyncTypeSettings(1f));
private void Awake()
{
_health.OnChange += on_health;
}
// This is called when _health changes, for server and clients.
private void on_health(float prev, float next, bool asServer)
{
/* Each callback for SyncVars must contain a parameter
* for the previous value, the next value, and asServer.
* The previous value will contain the value before the
* change, while next contains the value after the change.
* By the time the callback occurs the next value had
* already been set to the field, eg: _health.
* asServer indicates if the callback is occurring on the
* server or on the client. Sometimes you may want to run
* logic only on the server, or client. The asServer
* allows you to make this distinction. */
}Last updated