# Stats

**URL:** https://heroiclabs.com/docs/hiro/unity/stats/
**Keywords:** stats, hiro
**Categories:** hiro, unity, stats

---


# Stats

Read more about the Stats system in Hiro [here](../../concepts/stats/).

## Initializing the stats system

The stats system relies on the [Nakama System](../getting-started/nakama-system) and an `ILogger`, both must be passed in as dependencies via the constructor.

```csharp
var statsSystem = new StatsSystem(logger, nakamaSystem);
systems.Add(statsSystem);
```

## Subscribing to changes in the stats system

You can listen for changes in the stats system so that you can respond appropriately, such as updating the UI, by implementing the `IObserver` pattern, or use the `SystemObserver<T>` type which handles it for you.

```csharp
var disposer = SystemObserver<StatsSystem>.Create(statsSystem, system => {
  Instance.Logger.Info($"System updated.");

  // Update UI elements etc as necessary here...
});
```

## Refreshing the stats system

To ensure the stats system has the latest information from Nakama you can refresh it.

```csharp
await statsSystem.RefreshAsync();
```

## Listing public stats

You can list public stats for the user:

```csharp
foreach (var (key, value) in statsSystem.PublicStats)
{
    Debug.Log($"{key}: {value.Value}");
}
```

## Listing private stats

You can list private stats for the user:

```csharp
foreach (var (key, value) in statsSystem.PrivateStats)
{
    Debug.Log($"{key}: {value.Value}");
}
```

## Updating public stats

You can update public stats for the user.

```csharp
var stat = await statsSystem.PublicUpdateAsync("strength", 1, StatUpdateOperator.Delta);
```

{{< note "important" "Operators" >}}
There are 4 operator values that you can use to update stats:

- **Set**: Set the given value, overwriting any previous one.
- **Delta**: Increment or decrement the existing value by the given amount. Equivalent to Set if no previous value existed.
- **Min**: Use the new value if it's lower than the existing one. Equivalent to Set if no previous value existed.
- **Max**: Use the new value if it's higher than the existing one. Equivalent to Set if no previous value existed.
{{< / note >}}

## Updating private stats

You can update private stats for the user.

```csharp
var stat = await statsSystem.PrivateUpdateAsync("strength", 1, StatUpdateOperator.Delta);
```
## Additional information

- [Mage Mayhem sample project](../../../../sample-projects/games/mage-mayhem)
