I am using unity 5.0.2 and have a Text component to display my score and highscore on screen not a GUI. I have tried many different methods to get the GUI to work but all failed. My score works just fine when you collect coins it adds a point but the problem is when my timer hits 0 and restarts the game again I want to save and show my highscore and reset my score back to 0. I am in desperate need of help to fix this as it is for my app that is due to be released within the next two days. Please help?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public static int score;
public static int highscore;
Text text;
void Start()
{
text = GetComponent<Text> ();
score = 0;
highscore = PlayerPrefs.GetInt("highscore", highscore);
}
void Update()
{
if (score > highscore)
highscore = score;
text.text = "" + score; }
void ResetTime() {
PlayerPrefs.SetInt("highscore", highscore);
}
public static void AddPoints (int pointsToAdd)
{
score += pointsToAdd;
}
public static void Reset()
{
score = 0;
}
}
EDIT: Thanks to Hexer for pointing this out. You’re never calling ResetTime()
thus resulting in the highscore never getting saved. So when you go to load it on Start()
, it’s loading nothing because there’s nothing there to load. You’ll have to call ResetTime()
at the end of the game to save the highscore.
This code is pretty self explanatory. You can save and load to the computer without the use of
the insecure prefab saving system.
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections;
using UnityEngine.UI;
using UnityEngine;
using System.IO;
using System;
public class ScoreManager : MonoBehaviour
{
public int score;
public int highscore;
Text scoreText;
void Start ()
{
scoreText = GetComponent<Text>();
score = 0;
Load();
}
void Update ()
{
if(score > highscore)
{
highscore = score;
scoreText.text = highscore.ToString();;
}
}
public void AddScore (int addedScorePoints)
{
score += addedScorePoints;
}
public void ResetScore ()
{
score = 0;
}
public void Save ()
{
if(Directory.Exists(Application.dataPath + "/Save data/") == false)
Directory.CreateDirectory(Application.dataPath + "/Save data/");
BinaryFormatter bf = new BinaryFormatter ();
FileStream file = File.Create (Application.dataPath + "/Save data/Score.secure");
ScoreData data = new ScoreData ();
data.highscore = this.highscore;
bf.Serialize (file, data);
file.Close ();
}
public void Load ()
{
if(File.Exists(Application.dataPath + "/Save data/Score.secure"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/Save data/Score.secure", FileMode.Open);
ScoreData data = (ScoreData)bf.Deserialize(file);
file.Close();
this.highscore = data.highscore;
scoreText.text = highscore.ToString();
}
}
}
[Serializable]
class ScoreData
{
public int highscore;
}
All we do in this code is save out highscore to a file, then load it again when needed.
I would make your Manager a singleton and make it persist between scenes.
Like this:
public class ScoreManager : MonoBehaviour
{
public static ScoreManager instance;
public int score;
public int highscore;
public void OnEnable()
{
if (instance == null)
{
DontDestroyOnLoad(this.gameObject);//Make this Scoremanager object stay in all scenes
instance = this;//Make this ScoreManager accessible anywhere with ScoreManager.instance.score for example
highscore = PlayerPrefs.GetInt("HighScore",0);//0 if there isn't already a playerPref called "HighScore"
}
else
{
Destroy(this);//When you load a scene if you already have a ScoreManager that persists between scenes. Don't allow more than one to be created
}
}
public void Reset()
{
score = 0;
}
public void SaveScore()
{
PlayerPrefs.SetInt("HighScore",highscore);
}
public void AddPoints(int points)
{
score += points;
if (score > highscore)
highscore = score;
}
}
From there have your UI access the ScoreManager with ScoreManager.instance.score in a script on the UI that’s seperate from the ScoreManager
Call SaveScore when the game finishes and addPoints whenever the player gets points.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour {
public static int score;
public static int highscore;
Text text;
void Start()
{
text = GetComponent<Text> ();
score = 0;
highscore = PlayerPrefs.GetInt("highscore", highscore);
}
void Update()
{
if (score > highscore)
highscore = score;
text.text = "" + score; }
void ResetTime() {
PlayerPrefs.SetInt("highscore", highscore);
}
public static void AddPoints (int pointsToAdd)
{
score += pointsToAdd;
}
public static void Reset()
{
playerprefs.DeleteKey("score");
}
}