I’m trying to figure out how to change my script so that if the player gets a score higher than their previous high score it says New highscore. Could someone explain to me how I should do it, thanks. This is my bad attempt at it:
public bool newHiScore;
void Update()
{
if (Score > PlayerPrefs.GetInt("Highscore"))
{
PlayerPrefs.SetInt("Highscore", Score);
newHiScore = true;
}
else
if (Score <= PlayerPrefs.GetInt("Highscore"))
{
PlayerPrefs.SetInt("LatestScore", Score);
newHiScore = false;
}
}
This is from my ScoreSystem script ^^
and this is my Highscore Script:
using UnityEngine;
using UnityEngine.UI;
public class HighScoreScript : MonoBehaviour
{
public Text HiScore;
void Update()
{
bool newHiScore = true;
if (newHiScore)
{
HiScore.text = "New Highscore: " + PlayerPrefs.GetInt("Highscore").ToString();
}
bool newHiScore = false;
if(newHiScore)
{
HiScore.text = "Highscore: " + PlayerPrefs.GetInt("Highscore").ToString();
}
}
}
maybe something like this
HighScoreScript highScoreScipt;
public bool newHiScore;
void Update()
{
if (Score > PlayerPrefs.GetInt("Highscore"))
{
highScoreScipt.newHighScore();
}
else
if (Score <= PlayerPrefs.GetInt("Highscore"))
{
highScoreScipt.highScore();
}
public class HighScoreScript : MonoBehaviour
{
public Text HiScore;
void Update()
{
}
public void newHighScore()
{
HiScore.text = "New Highscore: " + PlayerPrefs.GetInt("Highscore").ToString();
}
public void highScore()
{
HiScore.text = "Highscore: " + PlayerPrefs.GetInt("Highscore").ToString();
}
}
}
its rough code so dont copy and paste it. it might have parsing errors but you get the gist of it.
What about the PlayerPrefs.SetInt(“latestScore”, Score); that’s what I use to show the player his/her score?
set them in the if statement in score system before the public methods from the high score script is called
Doesn’t work.
This is what the script looks like now:
void Update()
{
if (Score > PlayerPrefs.GetInt("Highscore"))
{
PlayerPrefs.SetInt("Highscore", Score);
HighScoreScript.manager.newHighScore();
}
else
if (Score <= PlayerPrefs.GetInt("Highscore"))
{
PlayerPrefs.SetInt("LatestScore", Score);
HighScoreScript.manager.highScore();
}
}
using UnityEngine;
using UnityEngine.UI;
public class HighScoreScript : MonoBehaviour
{
public static HighScoreScript manager;
public Text HiScore;
void Awake()
{
manager = this;
}
public void newHighScore()
{
HiScore.text = "New Highscore: " + PlayerPrefs.GetInt("Highscore").ToString();
}
public void highScore()
{
HiScore.text = "Highscore: " + PlayerPrefs.GetInt("Highscore").ToString();
}
The problem could be that the gameover screen fades in, thats why I was using void Update before not sure it will work like this. hmmm…
the highScoreScript doesnt need a reference to itself. the score system needs a reference to the highScoreScript class to access the public functions to change the text.
hiScoreScript = “whatever gameObject the scipt is on”.GetComponent();
if your having trouble with accessing script components you could simplify by moving everything to a single script.
I was told before by another member that, that’s the best way of accessing a component of another script because other methods are slower. :S