Advertisement
johnnygoodguy2000

ScoreManager.cs

Apr 27th, 2024 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.00 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 static int score;
  9.  
  10.     Text text;
  11.  
  12.     // Start is called before the first frame update
  13.     void Start()
  14.     {
  15.         text = GetComponent<Text>();
  16.  
  17.        
  18.         //score = 0;
  19.         score = PlayerPrefs.GetInt("PlayerCurrentScore");
  20.     }
  21.  
  22.     // Update is called once per frame
  23.  
  24.     void Update()
  25.     {
  26.        if (score < 0)
  27.        score =0;
  28.        text.text = "" + score;
  29.  
  30.     }
  31.  
  32.     public static void AddPoints(int pointsToAdd)
  33.     {
  34.         score += pointsToAdd; // Add points to the score
  35.  
  36.         PlayerPrefs.SetInt("PlayerCurrentScore", score); // Save the score in the PlayerPrefs to next level
  37.     }
  38.  
  39.     public static void Reset()
  40.     {
  41.         score = 0;
  42.  
  43.         PlayerPrefs.SetInt("PlayerCurrentScore", score); // Save the score in the PlayerPrefs to next level incase loss of points
  44.     }
  45. }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement