Tracking the Player’s Stats

We have the player collecting coins, but we’re not actually storing that data anywhere at the moment. We’re going to remedy that by creating a PlayerStats Component that will store information about the number of coins the player has collected and, eventually, will store the player’s total health value as well. Nothing can damage the player at this point, but we’ll be adding some hazards in the next chapter. In Chapter 10, “Creating the Menus and Interface Elements,” we’ll hook this information into the GUI so that the information is reflected on screen rather than only in the Inspector panel.

Create a new script named “PlayerStats” and add it to the Player GameObject in the usual fashion. Make sure the script is ...

Get Learning 2D Game Development with Unity®: A Hands-On Guide to Game Creation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.