- Home /
I am having trouble making a High score system. I am trying to write the the top 5 highscores to a file and cannot use PlayerPrefs ,I need help with a high score system
using System.Runtime.Serialization.Formatters.Binary; using System.Collections; using UnityEngine.UI; using UnityEngine; using System.IO; using System;
public class ScoreManger : MonoBehaviour { public static int score; Text Scoretext; public int highscore;
// Use this for initialization
void Awake () {
Scoretext = GetComponent<Text>();
score = 0;
Load();
highscore= new
}
// Update is called once per frame
void Update () {
Scoretext.text = "Score:" + score;
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();
}
}
} //private void LoadHighscore() //{ // StreamReader sr = new StreamReader("high_score.txt"); // highscore = Int32.Parse(sr.ReadLine()); // sr.Close(); //}
[Serializable] class ScoreData { public int highscore; } ,I using System.Runtime.Serialization.Formatters.Binary; using System.Collections; using UnityEngine.UI; using UnityEngine; using System.IO; using System;
public class ScoreManger : MonoBehaviour { public static int score; Text Scoretext; public int highscore;
// Use this for initialization
void Awake () {
Scoretext = GetComponent<Text>();
score = 0;
Load();
highscore= new
}
// Update is called once per frame
void Update () {
Scoretext.text = "Score:" + score;
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();
}
}
} //private void LoadHighscore() //{ // StreamReader sr = new StreamReader("high_score.txt"); // highscore = Int32.Parse(sr.ReadLine()); // sr.Close(); //}
[Serializable] class ScoreData { public int highscore; }
Your answer
![](https://koobas.hobune.stream/wayback/20220612075036im_/https://answers.unity.com/themes/thub/images/avi.jpg)