Where To Store Player Stats

I’ve been going through Unity Learn Premium and have learned so much about C# with this. Very useful. I’m making a mobile game where the player has lives, number of tries per level allowed, score per level, and an inventory of small boosters.

I’m trying to understand how this needs to be organized. Right now I have the player controller script only handle the movement of the player. I also created a Game Manager script that does simple things like start the game, restart the game, move the camera forward, etc.

Where do these player stats and inventory levels fit in to my code? Do I create another class in my Player Controller script or should stats & inventory be managed separate like the Game Manager?

Since it’s a single player game right now, I can create a PlayerStats class within the PlayerController, create an instance of PlayerStats and assign those values. But it’s not like I need to create multiple instances of lives, tries, and scores.

If I eventually upload this to the app store, I would like to manage and display a leaderboard with other players scores. From a high level perspective, how should individual stats / inventory levels from each player be organized, especially if I plan to store the inventory levels & scores of anyone who downloads and plays this game?

Can I use properties in the game manager and get and set these values there?

I would make a separate class that stores the player’s stats. You might even have a separate class that stores the cumulative after-game stats, which might have more fields than what you need in-game. For instance, you have score in game, but you might have highscore in the out-of-game structure, as well as total time played, etc.

You can use JSON to serialize any class into a string, then write that string to a text file. When you read that string back you can deserialize it back into the class and use it. Look up tutorials on using JSON. Unity contains a “tiny” JSON but I recommend instead getting the JSON .NET package from the asset store, as it is much fuller-featured and will handle pretty much any arbitrary data types you throw at it, whereas Unity’s is geared towards pre-shaped data.

What I ended up doing was creating a Data_Manager object and made sure it doesn’t get destroyed when new levels are loaded. I also created a UI_Manager. Pretty new here, so I want to keep things organized from the beginning as much as possible and I saw some games built out like this.

I’ll look into JSON. I’m familiar with it from using NodeJS for developing web applications, but never with C# in game development. Thanks for the feedback.

1 Like