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 usingPlayerPrefs.
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 thecurrentScore 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
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
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
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
8. Visual Learning: The Collection Pipeline
txt
9. Best Practices
-
PlayerPrefs is for Simple Data Only:
PlayerPrefsis 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
GameManagerobject into themanagerslot on your Coin prefab's Inspector. If the Coin tries to runmanager.AddPoints(10)but the slot is empty, the game will crash and screamNullReferenceException: 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.
Create a
GameManagerscript with apublic bool hasRedKey = false;.
- 2. Create a small red cube (The Key). Set it to Is Trigger.
-
3.
Write a script on the Key:
OnTriggerEnter -> set manager.hasRedKey = true -> Destroy(gameObject).
- 4. Create a large brown cube (The Door). Set it to solid (not trigger).
-
5.
Write a script on the Door using
OnCollisionEnter:
csharp
12. Practice Exercises
- 1. Why is it architecturally dangerous to store the player's total score variable directly on the Player's movement script?
- 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
GameManagerfor scoring versus storing data locally on pickups or the player character.
-
Q: Describe the limitations of
PlayerPrefs. Provide a scenario wherePlayerPrefsis 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 callstransform.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.