- Home /
Time goes to fast for the code. (C#)
I set up a timer and now I want to make it stop at zero but because its a float it always goes past 0 and doesn't stop the timer then goes into the negatives, this happens with my first code.
Code 1 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class timer : MonoBehaviour {
private Text textBox;
public float timeTaken;
// Use this for initialization
void Start () {
textBox = GetComponent<Text>();
}
// Update is called once per frame
void Update () {
if (timeTaken == 00.000) {
return;
}
timeTaken -= Time.deltaTime;
textBox.text = "" + timeTaken;
textBox.text=timeTaken.ToString("00.000");
}
}
Then my other code pauses it but its always like 0.002 to -0.006 and would only land on 0.000 some times.
Code 2 :
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class timer : MonoBehaviour {
private Text textBox;
public float timeTaken;
// Use this for initialization
void Start () {
textBox = GetComponent<Text>();
}
// Update is called once per frame
void Update () {
if (timeTaken <= 00.002) {
timeTaken = 0f;
return;
}
timeTaken -= Time.deltaTime;
textBox.text = "" + timeTaken;
textBox.text=timeTaken.ToString("00.000");
}
}
Is there anyway to make it stop at 0 all the time or will I have to deal with it being off only a little bit and does the frame rate have any factors on how fast the time goes??
it's not because it's too fast, but because you are removing from it a number more than 0.001 (Time.deltaTime). THe only way to fix that is by setting it to 0 manually
Answer by toddisarockstar · Jul 06, 2017 at 07:35 AM
Function update repeats itself when the screen display refreshes itself. unfortunatly if your user has a slower machine or if your code is heavy function update runs slower. so function update does not always run at a consistant time interval. most people mesure this as FPS. to fix the problem Time.deltatime is simply a measurement of time passing with each frame so you can multipy it to things when you want time or distance consistency in update. the float value of time.deltatime is usually not going to land exactly on zero at any point because it is a float so a == statement never catches usually . you have to use > or <. here is what i think you want to do:
void timer float;
void Start (){timer=5;}
void Update(){
if(timer>0){timer-=Time.deltaTime;
//this code keeps running before the timer
}else{
//this code is running after the timer
}
}
this is an example of a repeating timer:
timer-=Time.deltaTime;
if(timer<0){timer +=5f;
//this code runs only once every intervold
}
here is an exaple of a one time happening when a timer runs out:
void timer float;
void Start (){timer=5;}
void Update(){
if(timer>0){timer-=Time.deltaTime;
if (timer<=0){
//this would only happen once
}}
}
you will never get an even zero cause the frame is likely not going to end exactly on the time. if its that important to you. you can use intregers in FixedUpdate. cause Fixed Update runs cosistantly with time without the need for deltatime. and you can have your even zero when the timer runs out!!!
Answer by SohailBukhari · Jul 06, 2017 at 07:02 AM
will stop when time ends.
if (timeTaken <= 0)
{
timeTaken = 0;
}
and in the update your are decreasing time continuously.Check whether timeTaken is greater then zero
then execute timeTaken -= Time.deltaTime;
void Update()
{
if (timeTaken >= 0.01f)
{
timeTaken -= Time.deltaTime;
}
if (timeTaken <= 0)
{
timeTaken =0f;
}
}
Your answer
Follow this Question
Related Questions
Create a countdown in C# (inside Coroutine) 2 Answers
Couldn't resume once Time.timeScale is set to 0 1 Answer
Slider to modify a countdown time in another scene 0 Answers
Why does my MatchTime Class not work when using Properties? 1 Answer
Slider to modify a countdown time in another scene 0 Answers