# \[OnValueSynced] Attribute

It is often useful to know when a synchronized variable has changed its value. It can be easily achieved using the `OnValueSyncedAttribute`. This attribute lets you define a method that will be called each time a value of a synced member (field or property) changes in the non-simulated version of an entity.

### Usage

Let's start with a simple example:

```csharp
using Coherence.Toolkit;
using UnityEngine;
using UnityEngine.UI;

public class Player : MonoBehaviour
{
    [OnValueSynced(nameof(UpdateHealthLabel))]
    public float Health;

    public Text HealthLabel;

    public void UpdateHealthLabel(float oldHealth, float newHealth)
    {
        HealthLabel.text = newHealth.ToString();
        Debug.Log($"Player HP changed by: {newHealth - oldHealth}");
    }
}
```

Whenever the value of the `Health` field gets updated (synced with its simulated version) the `UpdateHealthLabel` will be called automatically, changing the health label text and printing a log with a health difference.

{% hint style="success" %}
For the OnValueSynced attribute to work, the given field or property has to be synchronized over the network, via the [Sync attribute](/manual/networking-state-changes/sync-and-command-attributes.md) or via the [configure window](/getting-started/setup-a-project/prefab-setup.md).
{% endhint %}

This comes in handy in projects that use authoritative [Simulators](/manual/simulation-server.md). The Client code can easily react to changes in the `Player` entity state introduced by the Simulator, updating the visual representation (which the Simulator doesn't need).

{% hint style="info" %}
Remember that the callback method will be called only for a **non-simulated** instance of an Entity. Use on a simulated (owned) instance requires calling the selected method manually whenever the value of a given field/member changes. We recommend using [properties with a backing field](https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/properties#properties-with-backing-fields) for this.
{% endhint %}

### Limitations

The `OnValueSynced` feature can be used only on members of user-defined types, that is, there's no way to be notified about a change in the value of a Unity type member, like `transform.position`. This might however change in the future, so stay tuned!


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.coherence.io/manual/networking-state-changes/value-sync-attribute.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
