Hello guys, i am new to unity .i am making a 2d topdown puzzle game . i am currrently having a little problem saving the high score. my game score system works like this... scorecount decreases when time(seconds) increases.the resultant decreased scorecount will be added to a base score which is set to 100. and then that score will be saved as high score if the score > highscore. in my case the highscore should be 0 at the beginning. the problem is that the highscore at the beginning is showing a random number of 3099 (i've tried resetting the playerprefs using Playerprefs.DeleteAll(); ). here is my scoremanager script. using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ScoreManager : MonoBehaviour { public Text scoreText; public Text BestScoreText; private float score; public float scoreCount; public float pointsPerSecond; // void start() // { // // // } void Update () { scoreCount += pointsPerSecond * Time.deltaTime; score = 100; } void FixedUpdate() { score = score + scoreCount; scoreText.text = "Your Score : " + Mathf.Round (score); BestScoreText.text = "Best Score : " + ((int)PlayerPrefs.GetFloat ("BestScore")); //PlayerPrefs.DeleteAll (); if (score > PlayerPrefs.GetFloat ("BestScore")) { PlayerPrefs.SetFloat ("BestScore", score); } } }
and another thing. whenever i increase the scorecount . default best score increases. like this ... if i set it to 10 ,best score showing 249, 100 means 1699, 200 means 3099. so what mistake i have been doing? and how to rectify it ?.thank you in advance.
Please check this forum post to show codes properly: https://forum.unity3d.com/threads/using-code-tags-properly.143875/ Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ScoreManager : MonoBehaviour { public Text scoreText; public Text BestScoreText; private float score; public float scoreCount; public float pointsPerSecond; // void start() // { // // // } void Update () { scoreCount += pointsPerSecond * Time.deltaTime; score = 100; } void FixedUpdate() { score = score + scoreCount; scoreText.text = "Your Score : " + Mathf.Round (score); BestScoreText.text = "Best Score : " + ((int)PlayerPrefs.GetFloat ("BestScore")); //PlayerPrefs.DeleteAll (); if (score > PlayerPrefs.GetFloat ("BestScore")) { PlayerPrefs.SetFloat ("BestScore", score); } } } You had two variables to keep the score. you also set the score variable to 100 every update, so it will always stay like the "random number" with your code. Instead of that, try this: Code (CSharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ScoreManager : MonoBehaviour { //For the UI public Text scoreText; public Text BestScoreText; //The actual score public float scoreCount; //Score to add every second public float pointsPerSecond; //You can also do this in Update() void FixedUpdate() { //Instead of having score and scoreCount, use only //scoreCount to make this calculation scoreCount += pointsPerSecond * Time.deltaTime; //ScoreCount is used to show the current score scoreText.text = "Your Score : " + Mathf.Round (scoreCount); //do this first to update the high score if (score > PlayerPrefs.GetFloat ("BestScore")) { PlayerPrefs.SetFloat ("BestScore", scoreCount); } //show the high score BestScoreText.text = "Best Score : " + ((int)PlayerPrefs.GetFloat ("BestScore")); } }