- Home /
How to have two of the same UI Text?
The title is a little confusing, but I want to have two texts displaying the player high score, one when he pauses the game, and the other displaying the hi score in the game over screen.
I made it work by the only way I currently know, but I guess its not a convenient and efficient way to do it. Script:
public class LevelManager : MonoBehaviour {
public static LevelManager instance;
public Pause pauseScript;
public GameObject gameOverText;
public Text scoreText;
public Text highScore;
public Text highScore2;
private int score = 0;
void Start() {
highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
highScore2.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
}
If you know the proper cleaner way to set it up, I appreciate it. Thank you in advance, have a great year :)
Store the score as a int variable ins$$anonymous$$d of playerprefs. If you want to reference it from other scripts, I'm not sure if GetComponent() will work in C#, it did in JS.
Answer by samth · Jan 08, 2019 at 01:22 AM
quickest fix to eliminate the redundant call:
highScore.text = PlayerPrefs.GetInt("HighScore", 0).ToString();
highScore2.text = highScore.text;
If you need to update the high score later you could instead create a separate method and grab the playerprefs data and call your method in Start:
void Start() {
int high = PlayerPrefs.GetInt("HighScore", 0);
SetHighScoreText(high);
}
void SetHighScoreText(int high) {
highScore.text = high.ToString();
highScore2.text = highScore.text;
}
Then you can call that method again whenever the player sets a new high score
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flat 2D bounce against gravity? 2 Answers
2D Terrain 1 Answer