Why does unity add with approximation when i increment in update ??

I need the variable to always stay constant at 0.02

That’s how floating point works; it’s nothing Unity in particular is doing. Use integers instead. Just display it as floating point when needed; e.g. “increment += 2; Debug.Log (increment/100.0f);”

However, this should definitely not be done in Update anyway, since you have no idea how long a frame will take, so you’re making the code framerate-dependent (which is bad).

–Eric

2 Likes

Unity adds floats using an approximation because the float type is an approximation by design. This is not unique to Unity. It is how storing an arbitrary number of decimal places using a fixed number of bits works.

As already mentioned, use integers instead.

1 Like