CodeBlanks Logo Icon CodeBlanks

Score system

Learn game progression with a script that tracks score.

Score System and Singleton Pattern

A central ScoreManager tracks points collected throughout the session. To allow other scripts effortless access, a static instance (Singleton pattern) is used.

public class ScoreManager : MonoBehaviour {
    public static ScoreManager instance;
    public int score = 0;
    void Awake() {
        instance = this;
    }
}

This allows any pickup script to register points directly with ScoreManager.instance.AddScore(10); in a single line.

Reflecting Score on UI Text

Whenever score increments, the screen user interface (UI) must reflect the updated total. The text property of the UI Text component is reassigned.

public void AddScore(int amount) {
    score += amount;
    scoreText.text = "Skor: " + score;
}

This offers immediate visual reward as players witness their points count increase on screen.

Practice Score update test

Define a Start method. Inside, declare int skor = 100; and print "Guncel skor: " + skor to the console.

Starter code

Player.cs
using UnityEngine;

public class ScoreTester : MonoBehaviour
{
    // Start icinde skor tanimla ve yazdir
}

Sample solution

Player.cs
using UnityEngine;

public class ScoreTester : MonoBehaviour
{
    void Start()
    {
        int skor = 100;
        Debug.Log("Guncel skor: " + skor);
    }
}

Expected output

Guncel skor: 100

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Which architectural approach allows other classes to access ScoreManager directly via 'ScoreManager.instance.AddScore(10)'?

  • Shader Pass
  • Singleton Pattern (static instance) Correct answer
  • Perlin Noise Filter
  • NavMesh Obstacle

Explanation: The Singleton pattern exposes a static instance reference granting global access to the object from any scene script.

2 Update UI score text

Question
scoreText.____ = "Skor: " + currentScore;
Answer
scoreText.text = "Skor: " + currentScore;

Explanation: The 'text' property is assigned to update the string rendered by the UI text component.

3 Score system terms

Term Meaning
ScoreManager Manager centrally tracking game score
AddScore Increments score and updates the UI
scoreText UI element displaying score text
static instance Global access reference to the class

All lessons in Advanced