- Home /
Question by
Strangerweather · Dec 05, 2015 at 05:28 PM ·
score system
How to set the High Score to be the Final Score?
Basically, I currently have a score and a high score. The score varies throughout the game, going up and down. The high score corresponds to the highest score achieved during the game. What I want is for the high score to be the final score that shows when the timer stops.
Here is my Score code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class ScoreManager : MonoBehaviour
{
public Text scoreText;
public Text finalScoreText;
public Text highScoreText;
public float scoreCount;
public float finalScoreCount;
public float highScoreCount;
public float minusPointsPerSecond;
public bool scoreIncreasing;
// Use this for initialization
void Start()
{
if (PlayerPrefs.HasKey ("HighScore"))
{
highScoreCount = PlayerPrefs.GetFloat("HighScore");
}
}
// Update is called once per frame
void Update()
{
if (scoreIncreasing)
{
scoreCount += minusPointsPerSecond * Time.deltaTime;
}
if (scoreCount > highScoreCount)
{
highScoreCount = scoreCount;
PlayerPrefs.SetFloat("HighScore", highScoreCount);
}
scoreText.text = "Score: " + Mathf.Round (scoreCount);
highScoreText.text = "High Score: " + Mathf.Round (highScoreCount);
}
public void ResetHighScore()
{
scoreCount = 0;
highScoreCount = 0;
highScoreCount = scoreCount;
PlayerPrefs.SetFloat("HighScore", highScoreCount);
}
public void AddScore (int pointsToAdd)
{
scoreCount += pointsToAdd;
}
}
And here is my Timer code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public float myTimer = 0;
public Text timerText;
public bool timerIsActive = true;
void Start()
{
timerText = GetComponent<Text>();
}
void Update()
{
if (timerIsActive)
{
myTimer += Time.deltaTime;
timerText.text = myTimer.ToString("f0");
if (myTimer <= 0)
{
myTimer = 0;
}
}
}
}
I'd be really grateful for some ideas on this. Many thanks!
Comment
I'm still hoping for some help if at all possible. Thanks!
Best Answer
Answer by Strangerweather · Dec 12, 2015 at 06:51 PM
Hi, for the record, I solved this by adding the following to line 38 of the ScoreManager:
if (!scoreIncreasing)
Quite easy then...
Your answer
![](https://koobas.hobune.stream/wayback/20220612051500im_/https://answers.unity.com/themes/thub/images/avi.jpg)