DEV Community

Discussion on: When coding C#, you need to use f for timeScale

Collapse
 
jayjeckel profile image
Jay Jeckel

The f is called a literal suffix and it doesn't mean "frame" it means float and it tells the compiler to treat the number literal as a float type. You have to use the f suffix because Time.timeScale is a float typed variable and double type literals can't be automatically converted to float.

// Works, because 1 is an int and int auto-casts
// to float because float is bigger than int.
Time.timeScale = 1;

// Doesn't work, because 1.0 is a double and double
// won't auto-cast to float since float is smaller than double.
Time.timeScale = 1.0;

// Works, because 1.0f is a float and doesn't need to be cast.
Time.timeScale = 1.0f;

// Works, despite 1.0 being a double because we are
// explicitly casting it to a float.
Time.timeScale = (float)1.0;
Enter fullscreen mode Exit fullscreen mode

It has nothing to do with Unity, units, or time scales, it's just basic operation of a strongly typed language.