O'Reilly logo

Unity Game Development Scripting by Kyle D'Aoust

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The stat tracker

Our next step is to give our stat tracker all the functionalities that we need. Here, we will create methods to set and reset stats, set and reset PlayerPrefs, and save PlayerPrefs. Finally, we create a way to show our stats on the screen. Playerprefs are functions built in Unity that allow storage of strings, integers, and floats using a system similar to Dictionary or KeyValuePair.

Setting the stats

The first function that we will create will allow us to set values to specific stats. Add this function to your script:

void SetStat(string stat, int intValue = 0) { switch(stat) { case "Kills": pKills+= intValue; float fKills = pKills; float fDeaths = pDeaths;if(fKills != 0) pKDR = fDeaths / fKills; break; case "Deaths": pDeaths+= ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required