Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.UI;
- public class ScoreDataManager : MonoBehaviour {
- public Text txtScore;
- public static int score=0;
- public Text txtHighscore;
- public static int highscore=0;
- int nameSize = 15;
- private float timer;
- void Start (){
- }
- //Grabbing the previous highscore from PlayerPrefs to static variable
- //when this script is Enabled(Like when Scene loaded)
- void OnEnable(){
- highscore = PlayerPrefs.GetInt("highscore");
- }
- //Saving the data from static variable highscore to Playerprefs when script is disabled(Game end or scene changed)
- void OnDisable(){
- StoreHighscoreInPlayerPrefs ();
- }
- void Update (){
- timer += Time.deltaTime;
- if (timer > 5f) {
- score += 5;
- timer = 0;
- }
- txtHighscore.text = string.Format("TOTAL POINTS: <size=" + nameSize + "><color=#00ffd8>{0}</color></size>", highscore);
- txtScore.text = string.Format("CURRENT POINTS: <size=" + nameSize + "><color=#00ffd8>{0}</color></size>", score.ToString());
- //Update the static variable highscore here only
- if (score > highscore)
- highscore = score;
- }
- void StoreHighscoreInPlayerPrefs ()
- {
- //Save the value of highscore static variable to Playerprefs
- PlayerPrefs.SetInt ("highscore", highscore);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement