I’m looking for an extra set up troubleshooting eyes. I’m creating a horizontal slider that should be centered at the bottom of my screen and be 1/2 the screen width in length. I’ve created vertical sliders with the same requirements on the left and right of the screen and those worked great - so I’m not sure what I’m doing wrong here. Also, you’ll notice I’m using (screen.width / 2) to center my slider which DID work properly, so I’m not sure why the length of the slider isn’t coming out properly.
Any help would be greatly appreciated. Here is my code:
using UnityEngine;
using System.Collections;
public class GUI : MonoBehaviour {
#region Variables
//Slider Values
public float sliderMin = 0.0f;
public float sliderMax = 360.0f;
public float xSliderValue;
//Slider Layout
//Default Rect Sizing
public int horzSliderLength = (Screen.width / 2);
public int horzSliderWidth = 15;
//General GUI Layout
public int screenMargin = 10;
public int sliderLabelWidth = 40;
public int sliderLabelHeight = 25; #endregion
opps, sorry. When I run the above code, I get a slider in the bottom middle of my screen (telling me the code is properly calculating screen.width / 2). However, the slider is only about 1/10th of my screen width rather than the 1/2 desired. Thus it is outputting an incorrect “horzSliderLength” value.
Well, you’re getting a bit of (untentional?) rounding here since you store the slider length in an INT:
public int horzSliderLength = (Screen.width / 2);
And then, further where you divide that value by 2 in several places, such as:
(horzSliderLength / 2)
What happens if you make horzSliderLength a float (and ensure float division everywhere)? I really don’t think that’s it, but it’s not jumping out at me. Maybe we need another set of eyes…
Thank you so much for the back and forth while taking a peek with me! I swear this is a case of something simple that I’m just not seeing or basic understanding I’m forgetting.
Nice thought on the float. I didn’t think it would solve this (as my vertical sliders follow the same declarations and work fine), but I tried it anyway. Unfortunately, didn’t work.
I thought it might be the fact that I was dividing it by 2 again somewhere else too, however, that shouldn’t be the case where you’re seeing it. The horizontal slider is defined as “(left start positioning, top start positioning, slider length, slider width)”. The only time I divide horzSliderLength is in the second positioning of the left start, as dividing it by two makes sure it is centered on the screen. If you notice, my third case is simply horzSliderLength… thus the slider length SHOULD BE = Screen.width / 2.
So I’m still at a loss. Thank you for the suggestions though! If anything else pops into your help feel free to give me a shout. I’m willing to try anything right now.