Question by
thefilmmaker · May 23, 2016 at 08:27 PM ·
guiscript.timerhighscores
Timer Board.
Hello i am trying to make a High score, based on time instead of score can you help :D
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class Timer : MonoBehaviour {
public Text timertext;
private float StartTimer;
public Text HighScore;
void Start () {
StartTimer = Time.time;
HighScore.text = "HighScore: " + PlayerPrefs.GetInt ("HighScore");
}
// Update is called once per frame
void Update () {
float T = Time.time - StartTimer;
string Min = ((int)T / 60).ToString ();
string Sec = (T % 60).ToString ("f2");
timertext.text = Min + ":" + Sec;
}
void StoreHighScore (int NyHighScore){
int oldHighscore = PlayerPrefs.GetInt ("HighScore", 0);
if (NyHighScore > oldHighscore)
PlayerPrefs.SetInt ("HighScore", NyHighScore);
}//StoreHighscore }//end
Comment
Answer by thefilmmaker · Jun 02, 2016 at 06:33 PM
Hello i have change my script a little :
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class Timer : MonoBehaviour {
private string timesetter;
//-------------------------------------------------
public Text timertext;
public Text Highscore;
private float StartTimer;
//-------------------------------------------------
private float timer = 0.00f;
public float Besttime = 0;
//public Text HighScore;
//-------------------------------------------------
void Start () {
//--------Starttimer-------------------------------
StartTimer = Time.time;
//--------HighScore--------------------------------
//HighScore.text = "HighScore: " + PlayerPrefs.GetInt ("HighScore");
}
//-------------------------------------------------
void Update () {
float T = Time.time - StartTimer;
string Min = ((int)T / 60).ToString ();
string Sec = (T % 60).ToString ("f2");
timesetter = Min + ":" + Sec;
timertext.text = timesetter;
//-------------Highscore--------------------------------
if (Besttime > StartTimer) {
//Besttime = StartTimer;
Highscore.text = Min + ":" + Sec;
PlayerPrefs.Save ();
Debug.Log ("Time is " + timesetter);
}
}
//------------------------------------------------------
//void OnGUI ()
//{
// GUI.Box(new Rect(100, 100, 100, 100), "Highscore" + timer) ;
//}
//-------------------------------------------------
}
i cant seem to get the time to save the:
if (Besttime > StartTimer) {
//Besttime = StartTimer;
Highscore.text = Min + ":" + Sec;
PlayerPrefs.Save ();
Debug.Log ("Time is " + timesetter);
}
part is my "highscore". help! :D