Score Manager

Complete score management system with high score persistence, combo multipliers with decay, and score change events. Uses singleton pattern for easy global access.

File
ScoreManager.cs
Unity
2022.3+
Level
beginner
Size
3.3 KB · 156 lines
Added
2026-03-23
License
MIT
View source

No APIs that Unity 6.0 to 6.6 made obsolete or removed. How this is checked

What it adds to your GameObject

A MonoBehaviour component: attach it to a GameObject. This is the Inspector you get, read from the source: 6 serialized fields with their defaults.

Inspector
Script ScoreManager
Combo Settings
Combo Decay Time 3
Max Multiplier 10
High Score
High Score Key HighScore
Events
On Score Changed (int)
List is Empty
On High Score Beaten (int)
List is Empty
On Combo Changed (int)
List is Empty

When to use it

Essential for hyper-casual games, mobile arcade games, and any game that needs score tracking with combo multipliers. Use this when you need persistent high scores, formatted score display (1.5K, 2.3M), and a combo system that rewards consecutive scoring.

Setup

  1. Create an empty GameObject named ScoreManager in your scene
  2. Attach the ScoreManager script to it
  3. Set comboDecayTime and maxMultiplier in the inspector
  4. Call ScoreManager.Instance.AddScore(points) from gameplay scripts
  5. Connect onScoreChanged and onHighScoreBeaten events to your UI text elements

Features

  • Score tracking with events
  • High score persistence (PlayerPrefs)
  • Score multiplier support
  • Combo system with decay timer
  • Animated score display helper

Inspector fields

Every field Score Manager exposes in the Inspector, with the default it starts at.

FieldTypeDefaultGroup
Combo Decay TimecomboDecayTimefloat3Combo Settings
Max MultipliermaxMultiplierint10Combo Settings
High Score KeyhighScoreKeystring"HighScore"High Score
On Score ChangedonScoreChangedUnityEvent<int>No listenersEvents
On High Score BeatenonHighScoreBeatenUnityEvent<int>No listenersEvents
On Combo ChangedonComboChangedUnityEvent<int>No listenersEvents

Public API

What other scripts can call on ScoreManager.

MemberReturns
AddScore(int points)void
GetScore()int
GetHighScore()int
ResetScore()void
ResetCombo()void
SetComboDecayTime(float time)void
GetComboTimerNormalized()float
ClearHighScore()void
GetFormattedScore()string
GetFormattedHighScore()string
static FormatNumber(int number)string
static Instance propertyScoreManager
CurrentScore propertyint
HighScore propertyint
ComboMultiplier propertyint
ComboCount propertyint
ComboTimer propertyfloat

Requirements

  • Calls DontDestroyOnLoad, so the object survives scene loads. Place it once, in your first scene.

Common mistakes

The singleton uses DontDestroyOnLoad, so don't place it inside a scene that gets additively loaded or you'll get duplicates — the Awake check destroys extras but you'll see a brief frame flicker. The combo multiplier formula (1 + ComboCount / 5) uses integer division, so combos 1-4 all give 1x — adjust the divisor if you want faster multiplier scaling. PlayerPrefs persistence is not tamper-proof; for competitive games, use a server-side leaderboard instead.

Source code

ScoreManager.cs, 156 lines, MIT licensed. Paste it into a new C# file of the same name.

ScoreManager.cs
C#
using UnityEngine;
using UnityEngine.Events;

public class ScoreManager : MonoBehaviour
{
    public static ScoreManager Instance { get; private set; }

    [Header("Combo Settings")]
    [SerializeField] private float comboDecayTime = 3f;
    [SerializeField] private int maxMultiplier = 10;

    [Header("High Score")]
    [SerializeField] private string highScoreKey = "HighScore";

    [Header("Events")]
    public UnityEvent<int> onScoreChanged;
    public UnityEvent<int> onHighScoreBeaten;
    public UnityEvent<int> onComboChanged;

    public int CurrentScore { get; private set; }
    public int HighScore { get; private set; }
    public int ComboMultiplier { get; private set; } = 1;
    public int ComboCount { get; private set; }
    public float ComboTimer { get; private set; }

    private float comboTimerMax;
    private bool highScoreBeaten;

    private void Awake()
    {
        if (Instance != null && Instance != this)
        {
            Destroy(gameObject);
            return;
        }

        Instance = this;
        DontDestroyOnLoad(gameObject);

        HighScore = PlayerPrefs.GetInt(highScoreKey, 0);
        comboTimerMax = comboDecayTime;
    }

    private void Update()
    {
        if (ComboCount > 0)
        {
            ComboTimer -= Time.deltaTime;
            if (ComboTimer <= 0f)
            {
                ResetCombo();
            }
        }
    }

    public void AddScore(int points)
    {
        int earnedPoints = points * ComboMultiplier;
        CurrentScore += earnedPoints;

        // Combo
        ComboCount++;
        ComboTimer = comboTimerMax;
        ComboMultiplier = Mathf.Min(1 + ComboCount / 5, maxMultiplier);
        onComboChanged?.Invoke(ComboMultiplier);

        onScoreChanged?.Invoke(CurrentScore);

        // High score check
        if (CurrentScore > HighScore)
        {
            HighScore = CurrentScore;
            SaveHighScore();

            if (!highScoreBeaten)
            {
                highScoreBeaten = true;
                onHighScoreBeaten?.Invoke(HighScore);
            }
        }
    }

    public int GetScore()
    {
        return CurrentScore;
    }

    public int GetHighScore()
    {
        return HighScore;
    }

    public void ResetScore()
    {
        CurrentScore = 0;
        highScoreBeaten = false;
        ResetCombo();
        onScoreChanged?.Invoke(CurrentScore);
    }

    public void ResetCombo()
    {
        ComboCount = 0;
        ComboMultiplier = 1;
        ComboTimer = 0f;
        onComboChanged?.Invoke(ComboMultiplier);
    }

    public void SetComboDecayTime(float time)
    {
        comboDecayTime = Mathf.Max(0.5f, time);
        comboTimerMax = comboDecayTime;
    }

    public float GetComboTimerNormalized()
    {
        if (comboTimerMax <= 0f) return 0f;
        return Mathf.Clamp01(ComboTimer / comboTimerMax);
    }

    private void SaveHighScore()
    {
        PlayerPrefs.SetInt(highScoreKey, HighScore);
        PlayerPrefs.Save();
    }

    public void ClearHighScore()
    {
        HighScore = 0;
        PlayerPrefs.DeleteKey(highScoreKey);
        PlayerPrefs.Save();
    }

    public string GetFormattedScore()
    {
        return FormatNumber(CurrentScore);
    }

    public string GetFormattedHighScore()
    {
        return FormatNumber(HighScore);
    }

    public static string FormatNumber(int number)
    {
        if (number >= 1000000) return (number / 1000000f).ToString("F1") + "M";
        if (number >= 1000) return (number / 1000f).ToString("F1") + "K";
        return number.ToString();
    }

    private void OnDestroy()
    {
        if (Instance == this)
            Instance = null;
    }
}

Next step

Idle Income GeneratorPassive income system for idle/clicker games with offline earnings calculation, upgrade scaling, and prestige mechanics. Handles large numbers with K/M/B/T formatting.