🐾PurrNet Docs
Sync Type

SyncTimer

SyncTimer is a server-driven countdown timer that stays in lockstep across all clients. Declare it as a field on your NetworkBehaviour and call Start() — every client sees the same timeRemaining.

SyncTimer — all clients count down in lockstepSyncTimer
Server
10
Client A
+120ms lag
10
Client B
+120ms lag
10
Server tick broadcast

How it works

SyncTimer uses the server clock as the source of truth. When you call Start(duration) on the server, PurrNet records the server timestamp and syncs the timer state to all clients. Each client computes timeRemaining locally from that anchor timestamp and its own estimate of server time — so the display stays smooth at full frame rate without sending a packet every tick.

Late-joining clients receive the current timer state on connect and catch up instantly.

Properties

ParameterTypeDefaultDescription
timeRemainingfloat—Seconds left until the timer expires. Updated every tick by the server and synced to clients.
durationfloat—The total duration the timer was started with. Does not change after Start() is called.
normalizedProgressfloat—A 0–1 value representing how much of the timer has elapsed. 0 = just started, 1 = expired. Useful for driving progress bars and lerps.
isRunningbool—True between Start() and expiry (or Stop()). False when paused, stopped, or not yet started.
isPausedbool—True when the timer has been paused with Pause() and is waiting to be resumed.
onExpiredevent Action—Fires on all clients when timeRemaining reaches zero. Subscribe to trigger end-of-round logic, loot drops, etc.

Methods

ParameterTypeDefaultDescription
Start(float duration)void—Begins the countdown from the given duration in seconds. Syncs the start time to all clients immediately.
Stop()void—Halts the timer and resets timeRemaining to 0. Does not fire onExpired.
Pause()void—Freezes the countdown. isPaused becomes true. timeRemaining does not decrease while paused.
Resume()void—Continues the countdown from where it was paused. isPaused becomes false.

Basic usage

RoundTimer.cs
using PurrNet;
using UnityEngine;

public class RoundTimer : NetworkBehaviour
{
    [SerializeField] private float roundDuration = 120f;

    private SyncTimer _timer = new();

    private void Awake()
    {
        _timer.onExpired += OnTimerExpired;
    }

    protected override void OnSpawned(bool asServer)
    {
        base.OnSpawned(asServer);

        if (asServer)
            _timer.Start(roundDuration);
    }

    private void OnTimerExpired()
    {
        // Fires on ALL clients simultaneously
        if (isServer)
            GameManager.Instance.EndRound();

        HUD.ShowRoundOverBanner();
    }

    // Server can pause mid-round (e.g. during a cutscene)
    public void PauseRound()
    {
        if (!isServer) return;
        _timer.Pause();
    }

    public void ResumeRound()
    {
        if (!isServer) return;
        _timer.Resume();
    }
}

Situational example — round timer with UI progress bar

normalizedProgress maps the elapsed time to a 0–1 range, making it trivial to drive a UI progress bar, a radial countdown, or any other visual that needs to sweep from full to empty as time runs out.

RoundTimerUI.cs
using PurrNet;
using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class RoundTimerUI : NetworkBehaviour
{
    [SerializeField] private RoundTimer    _roundTimer;
    [SerializeField] private Slider        _progressBar;
    [SerializeField] private TextMeshProUGUI _timerLabel;
    [SerializeField] private Image         _barFill;

    [SerializeField] private Color _normalColor  = Color.green;
    [SerializeField] private Color _urgentColor  = Color.red;
    [SerializeField] private float _urgentThreshold = 0.8f; // 80% elapsed

    private SyncTimer Timer => _roundTimer.Timer; // expose field via property

    private void Update()
    {
        if (!Timer.isRunning) return;

        // normalizedProgress goes 0 → 1 as time elapses
        float progress = Timer.normalizedProgress;
        float remaining = Timer.timeRemaining;

        // Update progress bar — inverted so it depletes left to right
        _progressBar.value = 1f - progress;

        // Format MM:SS
        int minutes = Mathf.FloorToInt(remaining / 60f);
        int seconds = Mathf.FloorToInt(remaining % 60f);
        _timerLabel.text = $"{minutes:00}:{seconds:00}";

        // Flash red when time is almost up
        _barFill.color = progress >= _urgentThreshold
            ? Color.Lerp(_normalColor, _urgentColor, (progress - _urgentThreshold) / (1f - _urgentThreshold))
            : _normalColor;
    }
}

Pause and resume example

CutsceneManager.cs
using PurrNet;
using System.Collections;

public class CutsceneManager : NetworkBehaviour
{
    [SerializeField] private RoundTimer _roundTimer;
    [SerializeField] private float      _cutsceneDuration = 5f;

    // Server triggers cutscene, pauses game timer during playback
    public void PlayVictoryCutscene(PlayerID winner)
    {
        if (!isServer) return;
        StartCoroutine(CutsceneCoroutine(winner));
    }

    private IEnumerator CutsceneCoroutine(PlayerID winner)
    {
        _roundTimer.PauseRound();
        RpcPlayCutscene(winner); // ObserversRpc to show video on all clients

        yield return new WaitForSeconds(_cutsceneDuration);

        _roundTimer.ResumeRound();
    }

    [ObserversRpc]
    private void RpcPlayCutscene(PlayerID winner)
    {
        CutscenePlayer.Play(winner);
    }
}

SyncTimer is server-driven

Only the server should call Start(), Stop(), Pause(), and Resume(). Clients have read-only access to the timer state. If a client needs to start a timer (e.g. a player-triggered event), use a [ServerRpc] to request it.

Never use Time.time for networked timers

Time.time runs independently on each machine and drifts over the session. Two clients starting at different wall-clock moments will show different countdowns after a minute. Always use SyncTimer (or another server-anchored mechanism) for any timer that must appear identical on all clients.