I want to display highest score by Y axis but my Y position varies

I’m using

        int score = Mathf.RoundToInt(player.position.y);
        scoreText.text = score.ToString();

and lets say I reach 30 score and I keep jumping on same platform my score varies when jumping to 28,29 and then reach 30, but when falling it goes 30,29,28… So how can i make it to show only 30 ?

Keep a separate value and only copy the height to that value when it is greater.

That separate value would be your actual score.

It sounds like Score = Highest Y-Value. If so:

private int _score;

void Update() {
    _score = Mathf.Max(_score, Mathf.RoundToInt(player.position.y));
}

As Y increases, _score increases, but if Y decreases, _score won’t go down.

1 Like

Thanks for fast and usefull replies :slight_smile: