what is the best way to set a high score for different scenes I have been working on playerprefs and binary formatter for days now I have tried to save the high score on my score text here
using System.Collections;
using System.Collections.Generic;
using System.Net;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System;
public class Score : MonoBehaviour
{
public float score = 0.0f;
public float highscore;
public float difficultyLevel = 1;
public float maxDifficultyLevel = 10;
public float scoreToNextLevel = 10;
public Scene scene;
public bool isDead = false;
public Text scoreText;
public DeathMenu deathMenu;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if (isDead)
return;
if (score > highscore)
{
highscore = score;
scoreText.text = highscore.ToString(); ;
}
if (score >= scoreToNextLevel)
LevelUp();
score += Time.deltaTime * difficultyLevel;
scoreText.text = ((int)score).ToString();
}
public void AddScore(int addedScorePoints)
{
score += addedScorePoints;
}
void LevelUp()
{
if (difficultyLevel == maxDifficultyLevel)
return;
scoreToNextLevel *= 2;
difficultyLevel++;
GetComponent<playermotor>().SetSpeed(difficultyLevel);
}
public void Save()
{
if (Directory.Exists(Application.dataPath + "/Save data/(scene.name)/") == false)
Directory.CreateDirectory(Application.dataPath + "/Save data/(scene.name)/");
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Create(Application.dataPath + "/Save data/(scene.name)/Score.secure");
ScoreData data = new ScoreData();
data.highscore = this.highscore;
bf.Serialize(file, data);
file.Close();
}
public void OnDeath()
{
isDead = true;
deathMenu.ToggleEndMenu(score);
}
[Serializable]
class ScoreData
{
public float highscore;
}
}
I need it to save a separate high score for each scene which i tried to do with scene.name and I am trying to load the score on my level select screen here
public class LevelSelect : MonoBehaviour
{
public Text foresthscoreText;
public Text deserthscoreText;
public float score;
public float highscore;
// Start is called before the first frame update
void Start()
{
}
public void Load()
{
if (File.Exists(Application.dataPath + "/Save data/(scene.name)/Score.secure"))
{
BinaryFormatter bf = new BinaryFormatter();
FileStream file = File.Open(Application.dataPath + "/Save data/(scene.name)/Score.secure", FileMode.Open);
ScoreData data = (ScoreData)bf.Deserialize(file);
file.Close();
this.highscore = data.highscore;
foresthscoreText.text = highscore.ToString();
}
}
I have been searching and trying on my own for days if anyone could help I would really appreciate it