Ok so, I’m a complete beginner making a game for school project. All I’ve done is followed tutorials. But with the different tutorials obviously some changes need to be made so you can incorporate other stuff from other tutorials in. Well, I was following a video on how to make score as well as high score, but the thing is my Game Over screen is in a completely different scene, with that in mind I tried to use playerprefs, now I can display my score on the game over screen but for some reason my High score doesn’t change and I’ve tried what ever I could using ChatGPT, other videos etc-
I made 3 scripts basically, score manager, score handler, and saving score:
Here they are in that order:
using UnityEngine;
using TMPro;
public class ScoreManager : MonoBehaviour
{
public TextMeshProUGUI scoreValueText;
public float scoreValue = 0f;
public float pointIncreasedPerSecond = 1f;
public TMP_Text highScoreText;
void FixedUpdate()
{
scoreValueText.text = ((int)scoreValue).ToString();
scoreValue += pointIncreasedPerSecond * Time.fixedDeltaTime;
}
}
using UnityEngine;
using TMPro;
using TMPro.Examples;
public class ScoreHandler : MonoBehaviour
{
public TMP_Text scoreValue;
public TMP_Text highscore;
private TextMeshProUGUI scoreValueText;
private TextMeshProUGUI highscoreText;
void Start()
{
scoreValueText = scoreValue.GetComponent<TextMeshProUGUI>();
highscoreText = highscore.GetComponent<TextMeshProUGUI>();
scoreValueText.text = PlayerPrefs.GetString("scoreValue", "0").ToString();
int scorevalue = PlayerPrefs.GetInt("scoreValue", 0);
int highscores = PlayerPrefs.GetInt("highscore", 0);
if (scorevalue > highscores)
{
PlayerPrefs.SetInt("highscore", scorevalue);
highscore = scoreValue;
highscoreText.text = PlayerPrefs.GetInt("highscore", 0).ToString();
}
}
}
using UnityEngine;
using TMPro;
public class Savingscore : MonoBehaviour
{
public TMP_Text scoreValue;
public TextMeshProUGUI scoreValueText;
private void Start()
{
PlayerPrefs.SetString("scoreValue", "0");
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Obstacle")
{
int scoreValue = PlayerPrefs.GetInt("scoreValue", 0);
PlayerPrefs.SetString("scoreValue", scoreValueText.text);
PlayerPrefs.SetString("highscore", scoreValueText.text);
PlayerPrefs.Save();
}
}
}
There are 2 scenes, Start Game (where you play) and End Screen (when you die), the score manager is in the 1st scene and the saving score is within the player/character while the score handler is in the second scene…
Can someone please help me out with where I’m going wrong- I have been stuck on this for 2 days straight and I’m actually going to go mad…