Advertisement
HashZayed

Untitled

Sep 7th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class ScoreDataManager : MonoBehaviour {
  7.  
  8.     public Text txtScore;
  9.     public static int score=0;
  10.     public Text txtHighscore;
  11.     public static int highscore=0;
  12.  
  13.     int nameSize = 15;
  14.     private float timer;
  15.  
  16.     void Start (){
  17.        
  18.     }
  19.     //Grabbing the previous highscore from PlayerPrefs to static variable
  20.     //when this script is Enabled(Like when Scene loaded)
  21.     void OnEnable(){
  22.         highscore = PlayerPrefs.GetInt("highscore");
  23.     }
  24.     //Saving the data from static variable highscore to Playerprefs when script is disabled(Game end or scene changed)
  25.     void OnDisable(){
  26.         StoreHighscoreInPlayerPrefs ();
  27.     }
  28.     void Update (){
  29.  
  30.         timer += Time.deltaTime;
  31.  
  32.         if (timer > 5f) {
  33.             score += 5;
  34.  
  35.             timer = 0;
  36.         }
  37.  
  38.         txtHighscore.text = string.Format("TOTAL POINTS: <size=" + nameSize + "><color=#00ffd8>{0}</color></size>", highscore);
  39.         txtScore.text = string.Format("CURRENT POINTS: <size=" + nameSize + "><color=#00ffd8>{0}</color></size>", score.ToString());
  40.         //Update the static variable highscore here only
  41.         if (score > highscore)
  42.             highscore = score;
  43.     }
  44.     void StoreHighscoreInPlayerPrefs ()
  45.     {
  46.         //Save the value of highscore static variable to Playerprefs
  47.         PlayerPrefs.SetInt ("highscore", highscore);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement