Skip to main content
Unity 3D Basics – Complete Beginner to Advanced Guide
CHAPTER 13 Beginner

Collectibles, Inventory, and Scoring Systems

Updated: May 16, 2026
30 min read

# CHAPTER 13

Collectibles, Inventory, and Scoring Systems

1. Introduction

A game without a goal is just a toy. Players need motivation, progression, and rewards. Whether it is collecting 100 golden rings, managing a backpack full of health potions, or achieving a high score, tracking data is fundamental to game design. In this chapter, we will master Collectibles, Inventory, and Scoring Systems. We will learn how to architect a centralized Game Manager, build a pickup system using Triggers, update the UI with collected loot, and permanently save the player's high score to their hard drive using PlayerPrefs.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Architect a centralized data tracking script (Game Manager).
  • Create collectible items that destroy themselves upon pickup.
  • Implement a basic List-based inventory system.
  • Persistently save and load data using PlayerPrefs.
  • Create a High Score system that survives game reboots.

3. The Data Manager Architecture

Where should the currentScore variable live?
  • Bad Idea: Putting it on the Coin script. If you collect the coin and destroy it, the score is destroyed!
  • Bad Idea: Putting it on the Player script. If the player falls in lava and is destroyed/respawned, the score is lost!
  • Good Idea: Create an Empty GameObject called GameManager. Attach a script to it that holds the score. It acts as an independent referee overseeing the match.
csharp
1234567891011
public class GameManager : MonoBehaviour
{
    public int currentScore = 0;
    public TextMeshProUGUI scoreText;

    public void AddPoints(int amount)
    {
        currentScore += amount;
        scoreText.text = "Score: " + currentScore;
    }
}

4. Creating the Collectible

The Coin's only job is to detect the player, tell the GameManager to add points, and then delete itself.
csharp
1234567891011121314
public class CoinPickup : MonoBehaviour
{
    // Drag the GameManager object into this slot in the Inspector
    public GameManager manager; 

    void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("Player"))
        {
            manager.AddPoints(10); // Give the player 10 points
            Destroy(gameObject);   // Vanish!
        }
    }
}

5. Basic Inventory (Lists)

If your game is an RPG, a simple integer score isn't enough. You need to know *what* the player collected (a sword, an apple, a key). We use C# List<string>. Unlike an Array, a List can grow and shrink dynamically as the player picks up and drops items.
csharp
123456789101112
using System.Collections.Generic; // Required for Lists!

public class InventoryManager : MonoBehaviour
{
    public List<string> backpack = new List<string>();

    public void AddItem(string itemName)
    {
        backpack.Add(itemName);
        Debug.Log("Added " + itemName + " to backpack. Total items: " + backpack.Count);
    }
}

6. Saving Data (PlayerPrefs)

If the player earns 500 points, closes the game, and opens it tomorrow, the score will be 0. Variables in RAM are cleared on exit. To save data permanently to the player's hard drive, Unity provides the incredibly simple PlayerPrefs class.
  • PlayerPrefs.SetInt("HighScore", 500) -> Saves the number to the hard drive.
  • PlayerPrefs.GetInt("HighScore", 0) -> Reads the number from the hard drive (and defaults to 0 if the file doesn't exist yet).

7. The High Score System

Let's combine scoring and saving to create an arcade high score system.
csharp
1234567891011121314
public void CheckHighScore()
{
    // 1. Load the old high score from the hard drive
    int savedHighScore = PlayerPrefs.GetInt("HighScore", 0);

    // 2. Did we beat it?
    if (currentScore > savedHighScore)
    {
        // 3. Save the new high score!
        PlayerPrefs.SetInt("HighScore", currentScore);
        PlayerPrefs.Save(); // Force write to disk
        Debug.Log("NEW HIGH SCORE: " + currentScore);
    }
}

8. Visual Learning: The Collection Pipeline

txt
1234567891011
[ Player touches Coin Collider (Trigger) ]
         |
[ Coin script calls manager.AddPoints(10) ]
         |
[ Coin calls Destroy(gameObject) ] 
         |
[ GameManager increases &#039;score' var in RAM ]
         |
[ GameManager updates UI Text on Canvas ]
         |
[ On Game Over, GameManager writes score to PlayerPrefs (Hard Drive) ]

9. Best Practices

  • PlayerPrefs is for Simple Data Only: PlayerPrefs is perfect for saving volume settings, level unlocks, and high scores. However, it is easily hackable by players and slow for massive data. If you are building a massive Skyrim-style RPG with 1,000 inventory items, you should learn to save data using JSON serialization instead.

10. Common Mistakes

  • NullReferenceExceptions: The most common error in this workflow is forgetting to drag the GameManager object into the manager slot on your Coin prefab's Inspector. If the Coin tries to run manager.AddPoints(10) but the slot is empty, the game will crash and scream NullReferenceException: Object reference not set to an instance of an object.

11. Mini Project: Build an RPG Key/Door System

Objective: Pick up a key, add it to an inventory, and use it to unlock a door.
  1. 1. Create a GameManager script with a public bool hasRedKey = false;.
  1. 2. Create a small red cube (The Key). Set it to Is Trigger.
  1. 3. Write a script on the Key: OnTriggerEnter -> set manager.hasRedKey = true -> Destroy(gameObject).
  1. 4. Create a large brown cube (The Door). Set it to solid (not trigger).
  1. 5. Write a script on the Door using OnCollisionEnter:
csharp
12345678910111213141516
void OnCollisionEnter(Collision hit)
{
    if (hit.gameObject.CompareTag("Player"))
    {
        // Check if the manager says we have the key!
        if (manager.hasRedKey == true)
        {
            Debug.Log("Door Unlocked!");
            Destroy(gameObject); // Open the door!
        }
        else
        {
            Debug.Log("The door is locked. Find the red key.");
        }
    }
}

12. Practice Exercises

  1. 1. Why is it architecturally dangerous to store the player's total score variable directly on the Player's movement script?
  1. 2. What C# collection type is best suited for an RPG inventory where items are constantly being added and removed?

13. MCQs with Answers

Question 1

To save a player's High Score permanently so it persists even after the computer is turned off, which Unity class should you use?

Question 2

When checking PlayerPrefs.GetInt("PlayerLevel", 1), what does the number 1 represent?

14. Interview Questions

  • Q: Explain the decoupling architecture of using a centralized GameManager for scoring versus storing data locally on pickups or the player character.
  • Q: Describe the limitations of PlayerPrefs. Provide a scenario where PlayerPrefs is the perfect tool, and a scenario where JSON serialization must be used instead.
  • Q: Walk me through the exact logic required to check an active score against a saved high score, update it if necessary, and ensure the data is written to the hard drive.

15. FAQs

Q: How do I make the items float and spin like in classic games? A: Attach a simple script to the item's mesh that calls transform.Rotate(0, 90 * Time.deltaTime, 0) in the Update loop!

16. Summary

In Chapter 13, we added progression to our game. We solved the architectural problem of where data should live by isolating it into a centralized GameManager. We used overlapping triggers to execute collection logic, utilizing dynamic C# Lists for inventory management. Finally, we bridged the gap between temporary RAM and permanent storage by mastering PlayerPrefs, allowing us to save, load, and overwrite High Scores that survive game reboots.

17. Next Chapter Recommendation

Our game is functionally complete. But if we want to play it on an iPhone, the mouse and keyboard won't work. Proceed to Chapter 14: Mobile Game Development Basics.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·