- Home /
Question by
AlekOstolle · Mar 09, 2018 at 05:02 PM ·
slidervaluecompare
Compare Slider.value to find the smallest slider ?
Hello, I have 5 sliders and their values. The value decrease (not at the same speed). I want to know which slider has the smallest value. could you help me ?
Comment
Answer by TreyH · Mar 09, 2018 at 05:39 PM
I think the simplest way would be to use a public array to assign those sliders from the inspector:
// This array is accessible from the inspector
public Slider[] sliders;
Go back to your inspector and assign the Size
to 5
, then select the sliders you want to compare. You can then get the slider with the smallest value by using something like:
// Use the public array above
public Slider CheckSmallestSlider ()
{
Slider minSlider = this.sliders [0];
for (int k = 1; k < this.sliders.Length; k++) {
if (minSlider.value > this.sliders [k].value)
minSlider = this.sliders [k];
}
return minSlider;
}
I'll attach the full script in a comment to this.
using UnityEngine;
using UnityEngine.UI;
public class SlideCheck : $$anonymous$$onoBehaviour
{
// This array is accessible from the inspector
public Slider[] sliders;
// Update is called once per frame
void Update ()
{
Slider $$anonymous$$Slider = this.CheckSmallestSlider ();
Debug.LogFormat ("{0} has the lowest value: {1}", $$anonymous$$Slider, $$anonymous$$Slider.value);
}
// Use the public array above
public Slider CheckSmallestSlider ()
{
Slider $$anonymous$$Slider = this.sliders [0];
for (int k = 1; k < this.sliders.Length; k++) {
if ($$anonymous$$Slider.value > this.sliders [k].value)
$$anonymous$$Slider = this.sliders [k];
}
return $$anonymous$$Slider;
}
}