Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ScoreManager : MonoBehaviour { public static int scoreCount; public static int highScoreCount; public Text scoreText; public GameObject zombie; public CanvasGroup score; // Start is called before the first frame update void Start() { //scoreCount = 0; // set the score to 0 at the start of the game highScoreCount = PlayerPrefs.GetInt("HighScore", highScoreCount); // store the highScore score.alpha = 1; score.interactable = true; score.blocksRaycasts = true; } // Update is called once per frame void Update() { if(GameObject.FindGameObjectWithTag(tag: "Zombie") == null) { scoreCount += scoreCount + 100; } scoreText.text = "Score: " + scoreCount.ToString(); // display the player's score if (scoreCount > highScoreCount) { scoreCount = highScoreCount; PlayerPrefs.SetInt("HighScore", highScoreCount); PlayerPrefs.Save(); } if (Time.timeScale == 0) { score.alpha = 0; score.interactable = false; score.blocksRaycasts = false; } } } This is working for each time an object with the "Zombie" tag is destroyed, but after they are destroyed the score always resets to zero? Could anyone help me find why this is happening? Is it a loop like I'm thinking I should do, and if so which kind? And if it isn't could a Coroutine work?
Double check your logic around here: Code (CSharp): if (scoreCount > highScoreCount) { scoreCount = highScoreCount; PlayerPrefs.SetInt("HighScore", highScoreCount); PlayerPrefs.Save(); } Specifically... I think Code (CSharp): scoreCount = highScoreCount; should be Code (CSharp): highScoreCount = scoreCount;
I can see a few problems here, however, your issue is that when your scoreCount is greater than the highScoreCount, you are assigning the value of highScoreCount to the scoreCount variable. I imagine you meant to do that the other way round.
@WarmedxMints and @PraetorBlue I think we pulled off a legitimate trifecta this morning! WOOT! (I hope that is all of OP's problems too... it's certainly ONE of the problems).
It fixed the score count resetting but now it goes to -100 when the gameObject tagged "Zombie" and only doing it once as well rather than every time something spawns with the "Zombie" tag. Despite those problems arising and persisting respectively, thank you for pointing out my mistake on line 36, there's only so much a solo developer can do without help from Unity's awesome community!
Rather than spritzling PlayerPrefs calls all over, you could use a construct like this: Here's an example of simple persistent settings using PlayerPrefs: https://pastebin.com/icJSq5zC Useful for a relatively small number of simple settings.