- Home /
C# code is making 0.0 a double and not a float
I'm trying to make a GUI Horizontal Slider. An error occurs where Unity thinks the min and max slider values (0.0, 20.0) are doubles and not floats. How do I fix this error?
walkSpeed = GUI.HorizontalSlider(new Rect(150, 40, 100, 20), walkSpeed, 0.0, 20.0);
Answer by Bunny83 · Oct 22, 2014 at 03:24 AM
C# doesn't "think" those are doubles, they are doubles :)
In C# the default real-literal type is double. In UnityScript it's float.
You just need to add an "f" at the end to make it a float literal. On the other hand if you need a double in UnityScript add a "d":
//C#
float a = 5.0f;
float b = 5f;
double c = 5.0d;
double d = 5d;
double e = 5.0;
//UnityScript
var f = 5.0; // will be a float
var g = 5.0d; // will be a double
Answer by Kiwasi · Oct 22, 2014 at 03:24 AM
UA ate my answer, again :(
0.0f, 20f
Its an artefact of the fact that doubles are better in almost all cases, so they are the C# default. 3D Game development is one of the rare cases where floats are better.
Your answer
Follow this Question
Related Questions
Getting a slider value from a different scene 1 Answer
GUI.HorizontalSlider precision 3 Answers
C# CS0266 error for multiplying by a decimal 1 Answer
Slider calculations go wrong 1 Answer
Slider value not working? 0 Answers