stop slider movement at specific value

i want to stop slider movement at specific value. from that value it cannot be move forward but allow its movement to back.

Okay? Going to need more info than that.

First off, this is not a support forum, your better of posting in a support forum such as scripting or UI as this is not a support forum.

Secondly, need context such as what have you tried doing, what worked what didnt work, etc? Your question is super vague and not sure how anyone can answer it properly.

Off top of my head and with the little info we have, I would say you want to run a bit of code in the “OnValueChanged” callback on a slider using Unity UI slider, and check what the new value is, if it is above the limit then set it to the limit, otherwise set it to the new value.

Something like:

public int valueUpperLimit;
int currentValue;

public void AdjustValue(int val)
{
    if(val > upperValueLimit) currentValue = upperValueLimit;
    else currentValue = val ;

}

And then hook that method up to the “OnValueChanged” Event field on the inspector. It will mean each time it changes its value it will call that function using the value as a parameter.

That should be all you need, based on the info given.

However you were not clear about how the slider works. Can it never go above the upper limit value, or can it sometimes? if it can never, what is the point of having a slider that goes beyond the selectable values?

If it can go beyond sometimes, in what circumstances?

Depending on that info, this may or may not be what you need.

Is it a slider bar that works as a loading bar?
Or a slider control bar like for example for volume?

I presume you mean a slider for accumulating something then when reaching a goal, resetting back to the beginning like a level progress bar?

If so, you could make a variables for progress (this variable would update the slider value as you progress forward), for the progress limits or goals (this would allow you to set where you want the slider to stop and reset the progress).

Make sure the slider value follows with the progress, then once your goal is achieved reset the slider and set progress back to zero or whatever it is in your context.

This is only a brief explanation and likely not the most efficient but if you specify what it is that you need, with maybe a little bit more context, I’ll happily help you :slight_smile:

1 Like

sorry about posting it here!
you didn’t pick my question. we have a slider its min and max values are 0 to 1 respectively.
i want if my slider reach at 0.5 value, it shouldn’t move on to max value “1”(should stop here). but if player wants to move it back to min value “0” then it should move back.

What I posted should work then, just have “valueUpperLimit” in my code above be 0.5 and your good to go. Good luck.

1 Like