Game Development Reference
In-Depth Information
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.
Player-
prefs
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+= intValue;
float fKills2 = pKills;
float fDeaths2 = pDeaths;if(fKills2 != 0)
pKDR = fDeaths2 / fKills2;
break;
case "TotalGold":
pTotalGold+= intValue;
break;
case "CurrentGold":
pCurrentGold+= intValue;