Reputation: 61
I tried to do something like
public double number = 10000;
public double decreasing = 10;
public void Update()
{
number -= decreasing * Time.deltaTime;
if (number == 0)
{
decreasing = 0;
}
}
but this doesn't work. How can I make it stop decreasing when it reaches 0?
Upvotes: 0
Views: 28
Reputation: 9824
This is a double, a form of rather big floating point number. Floats are known for their inprecision. A exact match like number == 0
happens once in a blue moon. Even after setting it to a value, it is not guaranteed to be precisely that value. This is a inherent, unavoidbale property of floating point types: https://www.youtube.com/watch?v=PZRI1IfStY0
Either expect it from now on and use <= checks, or stop using float altogether. Some simple code like this:
if(number <= 0)
number = 0;
will make sure that if it ever drops below 0, it will be seet back to 0. After this line, a exact match like number == 0
will be somewhat reliable. But my advice stands to just stop using a float altogether. A int where you display the last 3 digits as "decimal point" works way better here.
Upvotes: 2