- Home /
Help with SpeedRun personal best script
I created a script that on the script being enabled, a timer is enabled and when the goal is met, the script is disabled stopping the timer. What is supposed to happen is that once the script is disabled, it saves the current time and displays it on a different UI text? However, once I disable the script and enable it, the time restarts. How do I make it so that once the script is disabled, the time is saved and displayed on another text, and if I get a faster time, the text changes to the faster time.
This script is on the Text that tracks the time
using UnityEngine.UI;
public class Timer : MonoBehaviour
{
public Text timerLabel;
static public float time;
static public float bestTime;
void OnEnable()
{
time = 0f;
}
void Update()
{
time += Time.deltaTime;
bestTime = time;
var minutes = time / 60; //Divide the guiTime by sixty to get the minutes.
var seconds = time % 60;//Use the euclidean division for the seconds.
var fraction = (time * 100) % 100;
//update the label value
timerLabel.text = string.Format("{0:00} : {1:00} : {2:000}", minutes, seconds, fraction);
}
}
And this is the script that is on the game object which tracks the personal best
using UnityEngine.UI;
public class TimePersonalBest : MonoBehaviour {
public Text timerLabel;
static private float bestTimed;
static private float timed;
// Use this for initialization
void Start () {
timerLabel.text = string.Format("Personal Best:" + timed);
timed = TimeRun.time;
bestTimed = TimeRun.bestTime;
}
// Update is called once per frame
void Update () {
if(bestTimed <= timed)
{
timerLabel.text = string.Format("Personal Best:" + timed);
}
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612171431im_/https://answers.unity.com/themes/thub/images/avi.jpg)