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

Tracking stats

Now we will add the stat tracking feature that we created in Chapter 6, Keeping Score.

Adding the script

To allow stat tracking, we will simply drag the StatTracker script and drop it on the main camera. Now, to show the menu for stats, we will need to add an Update function. Add this to your script:

void Update()
{
  if(Input.GetKeyUp(KeyCode.E))
  {
    if(showStats)
      showStats = false;
    else
      showStats = true;
  }
}

Now when the player presses the E key, it will toggle the showStats Boolean variable. The stats menu will only show when showStats is set to true.

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