G
G
Gregory2562021-11-21 18:58:33
Unity
Gregory256, 2021-11-21 18:58:33

How to display HighScore in the menu?

Good day, please tell me how can I display the highscore in the menu (on another stage)?
There is such a script, hanging on the player

public class PlayrMovement : MonoBehaviour
{
private Rigidbody2D playerRb;
public float speed = 3;
private static int score;
private static int highScore;

public event UnityAction ScoreChanged;
public event UnityAction HighScoreChanged;

private void Start()
{
playerRb = GetComponent();
playerRb.GetComponent().gravityScale = 0;
highScore = PlayerPrefs.GetInt("highScore", highScore);
}
private void Update()
{
if (Input.GetKey(KeyCode.W))
{
playerRb.transform.Translate(Vector2.up * speed * Time.deltaTime);
IncreaseScore();
}
if (Input.GetKey(KeyCode.S))
{
playerRb.transform.Translate(Vector2.down * speed * Time.deltaTime);
}
HighScoreMethod();
}
public void IncreaseScore()
{
ScoreChanged?.Invoke(score);
score++;
}
public void HighScoreMethod()
{
HighScoreChanged?.Invoke(highScore);
if (score > highScore)
{
highScore = score;
PlayerPrefs.SetInt("highScore", highScore);
PlayerPrefs.Save();
}
}
}

it hangs on the UI
public TextMeshProUGUI _highScoreText;
public PlayerMovementplayer;

private void OnEnable()
{
player.HighScoreChanged += OnHighScoreChanged;
}
private void OnDisable()
{
player.HighScoreChanged -= OnHighScoreChanged;
}
private void OnHighScoreChanged(int highScore)
{
_highScoreText.text = highScore.ToString();
Debug.Log("Work");
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LittleBob, 2021-11-21
@Gregory256

It seems that everything is easy ...
You save the value in the prefs, which means that in void Start just pull out this value and write it to the text, only in a new scene. Do you have a ready-made script, or I misunderstood.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question