Book Image

Unity Game Development Scripting

By : Kyle D'Aoust
Book Image

Unity Game Development Scripting

By: Kyle D'Aoust

Overview of this book

Table of Contents (17 chapters)
Unity Game Development Scripting
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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+= intValue;

    float fKills2 = pKills;
    float fDeaths2 = pDeaths;if(fKills2 != 0)
      pKDR = fDeaths2 / fKills2;
    break;
  case "TotalGold":
    pTotalGold+= intValue;
    break;
  case "CurrentGold":
    pCurrentGold...