Problem with UI slider to make it increment other variables

Hi there, I’m relatively new to using Unity and I’m currently working on a simple 2d menu which involves a horizontal slider and buttons. Right now, I’m trying to increment an integer variable by 1 when the handle bar of the slider is dragged to the sliders max value. I’ve tried a few different things and it won’t seem to increment by 1, instead of incrementing by 1; the variable increments by a random number such as 1, 5, 6.

I’m doing the code with C# and using a dynamic float function to control the slider. When the slider reaches the max value, I want the handle bar to go straight back to the lowest value and that works fine, but once the slider moves back to the min value, this is when I want the i variable to increment by 1 but it increments by a random number instead.

my logic looks like this:

  public Slider mainSlider;
  public float sliderVal = 0f;
  bool validIncrement = false;
  int i = 0;
   
   public void setSlider(float slider)
    {
        sliderVal = slider;

        if (sliderVal == mainSlider.maxValue)
        {      
            mainSlider.normalizedValue = mainSlider.minValue;
        }

    
        if(sliderVal == mainSlider.minValue)
        {
            validIncrement = true;
        }
        else
        {
            validIncrement = false;
        }
    }

  

    public void submitValue()
    {
        Debug.Log("i: " + i);


        if (validIncrement)
        {
            i++;
        }
    }

Hi there, I’ll try to explain everything as simply as I can, but if there’s anything you don’t understand just ask and I’ll update this answer with more detail.


##Random Incrementing##

When your mouse is over the end of the slider, and your mouse is pressed, Unity tries to move the slider to the max value (as you don’t need to press on the handle of the slider itself to move it).


My Solution

Set up your slider as I’ve done below; the only things I’ve changed from the default is the slider only uses Whole Numbers, and the min and max values are 0 and 1 respectively. Then in the OnValueChange section of the slider, I’ve used itself as the object, and selected the method OnValueChange of my IncrementSlider script.


Now I’ll walk through the script;

  1. I can see from your above solution that you are using public variables; if you don’t need this variable to be accessed by other scripts, you can make the variable private, and instead use the Attribute SerializeField, to make it visible in the inspector. This variable I have set to the Slider that the script is attached to, but dragging it in the inspector.

  2. The m_myValue variable is your public int i variable

  3. The boolean m_increment, is what I’m using to allow the m_myValue variable to be incremented once the slider returns to the minimum value.

  4. The OnValueChange method, checks if the slider is being slid to the maximum value, and sets the m_increment variable to true

  5. In the Update method, we check if the mouse button 0 (left mouse button) has been released, and if the m_increment boolean is true. If these are both true, the slider’s current value will be set to the minimum value, and the m_myValue variable will be incremented.

    public class IncrementalSlider : MonoBehaviour
    

    {
    [SerializeField]
    private Slider m_slider;
    private int m_myValue;
    private bool m_increment;

    public void Update()
    {
        if (Input.GetMouseButtonUp(0) && m_increment)
        {
            m_slider.value = m_slider.minValue;
            m_myValue++;
        }
    }
    
    public void OnValueChange()
    {
        if (m_slider.value == m_slider.maxValue)
            m_increment = true;
    }
    

    }


Round Up

This solution doesn’t have a way to ‘cancel’ the action, but if you wanted one, then simply add an else section to the OnValueChange if statement, to set the m_increment bool to false.

Tip; if you want to see the values of private variables without the SerializeField attribute, you can select the debug mode for the inspector.

I hope this helped you.