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

Let's check the achievements


Next, we will add the functions that will actually check for achievements. These are the functions we will call when we want to check whether the player's stats have unlocked any achievements.

Checking a specific achievement

The CheckAchievement function will allow us to check for a single achievement. It takes a string, which is the achievement to check for. From here, it runs a switch statement to decide which achievement to modify. Add this function to your script. This function can be used when loading a menu, which shows the player's achievements and can be used to prevent unlocking the same achievement more than once:

void CheckAchievement(string Achievement)
{
  switch(Achievement)
  {
  case "Kills":
    Kills(PlayerPrefs.GetInt("PlayerKills"));
    break;
  case "TotalGold":
    TotalGold(PlayerPrefs.GetInt("PlayerTotalGold"));
    break;
  case "GoldSpent":
    GoldSpent(PlayerPrefs.GetInt("PlayerGoldSpent"));
    break;
  case "Level":
    Level(PlayerPrefs...