using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class Timer : MonoBehaviour
{
public Text timerText;
private float startTime;
public Text hightscore;
void Start()
{
startTime = Time.time;
hightscore.text = PlayerPrefs.GetString("HighScore");
}
void Update()
{
float t = Time.time - (startTime - (-3));
string minutes = ((int)t / 60).ToString();
string seconds = (t % 60).ToString("f2");
timerText.text = minutes + "." + seconds;
if (timerText.text > PlayerPrefs.GetString("HighScore")) // i get an error on this line!
{
PlayerPrefs.SetString("HighScore", timerText.text);
}
}
}
Your lines 23-25 make the time into a string.
You probably want to store it instead as a float, with PlayerPrefs.GetFloat/SetFloat
The error you see on line 27 is from comparing two strings. If you want to compare strings you can with the string’s in-built IComparer interface, but I am not sure it will get you the value-comparison result you want.
Best to keep and track your time entirely in floats: calculate it, store it, compare with it, etc., then when you want to display it, turn it into a string and format it for display.
Also, you can use string.Format() to format your text nicely.
I knew it was a silly mistake but i couldn’t see it anyway i fixed Thanks a lot man !
Have a great day!
1 Like