Question by
Bionicsonic · Nov 23, 2017 at 11:26 PM ·
timetimertimer countdown
How can I make this timer become longer over time? (When I click it memorizes then finishes it increases wait time)
private float msToWait = 5000.0f;
private Button button;
private ulong lastClick;
private Text timer;
private float counter = 1000.0f;
private void Start(){
button = GetComponent<Button>();
lastClick = ulong.Parse(PlayerPrefs.GetString("LastClick"));
timer = GetComponentInChildren<Text>();
if(!IsClickReady())
button.interactable = false;
}
private void Update(){
if(!button.IsInteractable()){
if (IsClickReady()){
button.interactable = true;
timer.text = "Ready!";
return;
}
//Set Timer
ulong diff = ((ulong)DateTime.Now.Ticks - lastClick);
ulong m = diff / TimeSpan.TicksPerMillisecond;
float secondsLeft = (float)(msToWait - m) / 1000.0f;
string r = "";
//Hours
r += ((int)secondsLeft / 3600).ToString()+"h ";
secondsLeft -= ((int)secondsLeft / 3600)*3600;
//Minutes
r += ((int)secondsLeft / 60).ToString("00")+"m ";
//Seconds
r += ((int)secondsLeft % 60).ToString("00")+"s"; ;
timer.text = r;
}
}
public void Click(){
lastClick = (ulong)DateTime.Now.Ticks;
PlayerPrefs.SetString("LastClick",DateTime.Now.Ticks.ToString());
button.interactable = false;
// This is where you can give rewards
}
private bool IsClickReady(){
ulong diff = ((ulong)DateTime.Now.Ticks - lastClick);
ulong m = diff / TimeSpan.TicksPerMillisecond;
float secondsLeft = (float)(msToWait + counter - m ) / 1000.0f; //Has to be a float value not an int value
if(secondsLeft < 0){
timer.text = "Ready!";
return true;
}
return false;
}
}
Comment
Your answer
Follow this Question
Related Questions
Clock doesn't stop when reaches 0.0. Can you help me with this. 1 Answer
Make an Object Stop Moving Upon Collision 0 Answers
Problems showing accurate time with FixedUpdate() 0 Answers
Need coroutine to render a texture value clamped from 0 to 1 over time 1 Answer
Progress bar/countdown with hours, minutes, and seconds? 0 Answers