Advertisement
johnnygoodguy2000

ScoreManager.cs

Apr 7th, 2024 (edited)
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.31 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5.  
  6. public class ScoreManager : MonoBehaviour
  7. {
  8.     public Text scoreText;
  9.     public Text highScoreText;
  10.  
  11.     public float scoreCount;
  12.     public float highScoreCount;
  13.  
  14.     public float pointsPerSecond;
  15.  
  16.     public bool scoreIncreasing;
  17.  
  18.     public bool shouldDouble;
  19.     // Start is called before the first frame update
  20.     void Start()
  21.     {
  22.        
  23.         if (PlayerPrefs.HasKey("HighScore"))
  24.         {
  25.             highScoreCount = PlayerPrefs.GetFloat("HighScore");
  26.         }
  27.     }
  28.  
  29.     // Update is called once per frame
  30.     void Update()
  31.     {
  32.         if(scoreIncreasing)
  33.         {
  34.             scoreCount += pointsPerSecond * Time.deltaTime;
  35.         }
  36.        
  37.        
  38.         if (scoreCount > highScoreCount)
  39.         {
  40.             highScoreCount = scoreCount;
  41.  
  42.             PlayerPrefs.SetFloat("HighScore", highScoreCount);
  43.         }
  44.  
  45.         scoreText.text = "Score: " + Mathf.Round(scoreCount);
  46.         highScoreText.text = "High Score: " + Mathf.Round(highScoreCount);
  47.     }
  48.  
  49.     public void AddScore(int pointsToAdd)
  50.     {
  51.  
  52.         if (shouldDouble)
  53.         {
  54.             pointsToAdd = pointsToAdd * 2;
  55.         }
  56.         scoreCount += pointsToAdd;
  57.  
  58.        
  59.     }
  60. }
  61.  
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement