Search Unity

[Help] Is there anyway to change a UI Text to a Float?

Discussion in 'Scripting' started by YouBungy, Jul 30, 2018.

  1. YouBungy

    YouBungy

    Joined:
    Jun 19, 2017
    Posts:
    32
    So, In my game i have a volume slider, but to make it actually change the value of the song you have to use a float. but im trying to convert the UI text that displays what the value of the slider to a float so i can use that to change the volume. heres the code for the script
    Code (CSharp):
    1.  
    2. public class VRSliderReadout : MonoBehaviour
    3.     {
    4.         public enum OutputField
    5.         {
    6.             X,Y,Z
    7.         }
    8.         public Text text;
    9.         public VRSlider slider;
    10.         public string prefix;
    11.         public string postfix;
    12.         public OutputField field;
    13.         public bool forVolume = false;
    14.         public AudioSource OrigenVolume;
    15.         public float OrigenVolumeFloat;
    16.         void Start()
    17.         {
    18.             if (text == null) text = GetComponent<Text>();
    19.             if (text == null) Debug.LogError("Slider Readout requires text reference to output to", gameObject);
    20.             slider.sliderEvent += SliderEvent;
    21.             SliderEvent(slider, new Vector3(slider.XPercent, slider.YPercent, slider.ZPercent));
    22.         }
    23.         void SliderEvent(object sender, Vector3 output)
    24.         {
    25.             string field = "";
    26.             switch(this.field)
    27.             {
    28.             case OutputField.X:
    29.                 field = (output.x*100).ToString("N0");
    30.                 break;
    31.             case OutputField.Y:
    32.                 field = (output.y*100).ToString("N0");
    33.                 break;
    34.             case OutputField.Z:
    35.                 field = (output.z*100).ToString("N0");
    36.                 break;
    37.             }
    38.             text.text = prefix + field + postfix;
    39.         }
    40.         void Update()
    41.         {
    42.             if (forVolume == true){
    43.                 text.text = OrigenVolumeFloat.ToString();
    44.                 OrigenVolume.volume = OrigenVolumeFloat;
    45.             }
    46.         }
    47.     }
    48. }
    49.  
    Any Help Would Be GREATLY Appreciated!
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,532
    Use the slider value and not the text. The text is just some display info for the user, your back end shouldn't care about it.
     
    Joe-Censored likes this.
  3. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Your slider should already be giving its value as a float.
     
  4. YouBungy

    YouBungy

    Joined:
    Jun 19, 2017
    Posts:
    32
    Thanks! Can’t believe I was dumb enough to not realize this