Answer the question
In order to leave comments, you need to log in
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();
}
}
}
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
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question