Set a Timer countdown from a TimeSpan
Hi! I've been dealing with this for two days and I don't know what else to try so, hoping someone can help. I have set a timer which shows the remaining hours until the player can access the Level again by comparing the time difference between GameOverTime and StartTime (both saved on PlayerPrefs; which I know it's not the safest option but it has to be like this for now). My problem is that I want that timer to keep counting down while it's being displayed on screen. And I don't know how to do that. Basically, if the remaining time is 5:15:08 I want it to keep going down from there to 5:15:07, 5:15:06, etc. I've tried using several things I've found online but none has worked so far :/
The latest thing I came up with was to provide the player with a button to update the time (kind of a "refresh page" thingy) but the way I have it, it doesn't work either...
Here is my code:
private bool unlocked; public string level;
public GameObject levelLocked;
public GameObject holder;
public GameObject holder2;
public GameObject holder3;
public GameObject timeLeftObject;
public DateTime startTime;
public DateTime GameOverTime;
public TimeSpan timeDifference;
public Text timer;
private String timeRemaining;
private TimeSpan timeDifference2;
public TimeSpan timeToPass = new TimeSpan (8,0,0);
public TimeSpan timeToRestart1;
// Use this for initialization
void Start () {
PlayerPrefs.SetInt ("Level1", 1);
startTime = DateTime.Now;
GameOverTime = Convert.ToDateTime(PlayerPrefs.GetString ("LastTimeGameOver"));
Debug.Log(GameOverTime);
timeDifference = startTime - GameOverTime;
timeToRestart1 = timeDifference.Subtract (timeToPass);
Debug.Log (timeDifference);
timer = GetComponent<Text> ();
timeRemaining = string.Format ("{0:D2}:{1:D2}:{2:D2}", Math.Abs(timeToRestart1.Hours), Math.Abs(timeToRestart1.Minutes), Math.Abs(timeToRestart1.Seconds));
Debug.Log ("Ha cambiado");
if (PlayerPrefs.GetInt ("Level21") == 1) {
holder.SetActive (false);
holder2.SetActive (true);
}
if (PlayerPrefs.GetInt ("Level41") == 1) {
holder.SetActive (false);
holder2.SetActive (false);
holder3.SetActive (true);
} else {
holder.SetActive (true);
holder2.SetActive (false);
holder3.SetActive (false);
}
}
// Update is called once per frame
void Update () {
if (timeDifference.Hours < 8) {
timeLeftObject.SetActive (true);
timer.text = timeRemaining;
Debug.Log ("Menor que 8 horas");
} if(timeDifference.Hours >= 8){
timeLeftObject.SetActive (false);
Debug.Log ("Más o igual a 8 horas");
}
}
public void SelectLevel (string level)
{
if (PlayerPrefs.GetInt(level) == 1){
unlocked = true;
SceneManager.LoadScene (level);
} else {
unlocked = false;
levelLocked.SetActive (true);
if (Input.GetKeyDown(KeyCode.Space)) {
levelLocked.SetActive (false);
unlocked = false;
}
}
}
public void UpdateTime(){
if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown(0)) {
timer.text = timeRemaining;
}
}
}
I'm a noob in programming so maybe the answer is really simple... Any help is very much appreciated! Thanks!
Spent 12 hours with that TimeSpan today. Sum up: forget it and get to simple float math ins$$anonymous$$d :)