- Home /
GUI.HorizontalSlider precision
Can HorizontalSlider be configured to only slide through whole numbers, 1, 2, 3, 4, 5... and not decimals, 0, 2.631579, 5.233158...
Thank you.
Answer by rd42 · May 21, 2010 at 03:14 PM
Here it is, I think. I had the var holding the slider value set as a float, when changed to int. All is well.
Any one know why some times when you paste code into this text box after pressing 101010 it doesn't format it right? See example below.
var velMagnitude: int = 0;
function OnGUI () { // Initial Velocity GUI.Label(Rect(3,300,200,50), "Initial Velocity"); velMagnitude = GUI.HorizontalSlider(Rect(3,320,200,20),velMagnitude,0,500); GUI.Label(Rect(220,320,50,20), velMagnitude.ToString()); }
You select your code, then press the code button, like I just did.
Answer by geniuscd · Sep 18, 2012 at 12:45 PM
in c#
public class sliderExample: MonoBehaviour{
float repairOmeter = 0.0f;
void OnGUI(){
GUI.Label(new Rect(10,20,100,20),"Repair Meter Over Time");
repairOmeter = GUI.HorizontalSlider(new Rect(25, 45, 100, 30), repairOmeter, 0.0F, 10.0F);
repairOmeter = (int)repairOmeter;//only for Int
}
}
hope this helps :), I guess this is exactly what you need :P
Answer by Tigrou777 · Apr 23, 2016 at 10:51 AM
Instead of casting float to int, I would rather suggest to use Mathf.Round(). In most cases it will slip snap the slider to a position closer to mouse cursor. Eg: 3.9 will be snapped to 4 instead of 3.
void OnGUI(){
value = GUI.HorizontalSlider(new Rect(...), repairOmeter, 0.0f, 10.0f);
value = Mathf.Round(value);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613013819im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
int myX = (int)transform.position.x; 1 Answer
C# divide float by integer 2 Answers
Why aren't these numbers going down by 1. 1 Answer
Floating Point and ToString issue 2 Answers
How to i set the int to hitpoints / 10 + the current int. 1 Answer