- Home /
Problem when trying to save time
Hello everyone!
I'm made a simple runtime timer for my camera. Now I'm trying to save the time, but it doesn't save.. Can someone give me some advice on what am I doing wrong here?
Any tip / advice is highly appreciated! Thank you very much! :)
P.S. Sorry if my english is bad.
#pragma strict
static var timer : float;
var timer_text : GameObject;
private var check : boolean = false;
function Awake ()
{
timer = PlayerPrefs.GetFloat("RecordingTime_timer");
}
function Update ()
{
if(!check)
{
var t : System.TimeSpan = System.TimeSpan.FromSeconds(timer);
timer_text.guiText.text = String.Format("{0:D2}:{1:D2}:{2:D2}:{3:D2}", t.Hours, t.Minutes, t.Seconds, t.Milliseconds);
timer += Time.deltaTime;
if(timer > PlayerPrefs.GetFloat("RecordingTime_timer"))
PlayerPrefs.SetFloat("RecordingTime_timer", timer);
wait();
}
}
function wait ()
{
check = true;
yield WaitForSeconds(0.3);
check = false;
}
Answer by AurimasBlazulionis · Mar 24, 2017 at 05:48 PM
You will never save the timer because your timer is always more than the saved time.
Your logic:
begin with:
timer = saved time
... then
timer += deltatime
... and
if timer < saved time, then saved time = timer
It worked! :) Thank you very much! I updated the script! The only problem now is that it's really unoptimized.. I tried to do something, but now it doesn't even count the time anymore.. Could you give me one more advice on this matter? :) Thank you!
Your answer
Follow this Question
Related Questions
Life System need help please!!! 1 Answer
PlayerPrefs file wrong stored location 0 Answers
Where to save player progress for playstore app? 1 Answer
Unity Custom Editor - save value 1 Answer
How to save color 3 Answers