- Home /
How to have one slider affect another slider's value?
So I have three sliders (in this case they are actually scrollbars, is slider and scrollbar really different?). I want the total value of all scrollbars = 1 (or 100%, whatever), so if S1 = .5, and S2 = .25, then S3 = .25, or .75/.25/0, etc... I want the other two scrollbars to adjust as the picked scrollbars moves, so if S1 is being slid, S2 and S3 increment or decrement their values as indicated by S1's value. I want this behavior to be consistent whether the player is moving S1, S2, or S3.
I thought that using the 'onValueChange' listener would do the trick, but my code keeps zeroing out the two non-selected sliders.
Here I set up the listeners:
void Start()
{
maint.onValueChanged.AddListener(delegate { updateSliderValues(0); });
res.onValueChanged.AddListener(delegate { updateSliderValues(1); });
growth.onValueChanged.AddListener(delegate { updateSliderValues(2); });
}
And here is the updateSliderValues method:
private void updateSliderValues(int slider)
{
float total = maint.value + res.value + growth.value;
float change = total - 1;
float halfChange = change / 2;
switch (slider)
{
case 0:
{
Debug.Log("Slider " + slider + " value has changed to " + maint.value );
if (maint.value == 1)
{
res.value = 0;
growth.value = 0;
}
else
{
if (change > 0)
{
if (res.value >= halfChange && growth.value >= halfChange)
{
res.value -= halfChange;
growth.value -= halfChange;
Debug.Log("both minus");
}
else if (res.value < halfChange)
{
growth.value -= (change - res.value);
res.value = 0;
Debug.Log("growth minus");
}
else
{
res.value -= (change - growth.value);
growth.value = 0;
Debug.Log("res minus");
}
}
else
{
res.value = res.value + halfChange;
growth.value = growth.value + halfChange;
}
}
break;
}
case 1:
{
Debug.Log("Slider " + slider + " value has changed to " + res.value);
if (res.value == 1)
{
maint.value = 0;
growth.value = 0;
}
else
{
if (change > 0)
{
if (maint.value >= halfChange && growth.value >= halfChange)
{
maint.value -= halfChange;
growth.value -= halfChange;
}
else if (maint.value < halfChange)
{
growth.value -= (change - maint.value);
maint.value = 0;
}
else
{
maint.value -= (change - growth.value);
growth.value = 0;
}
}
else
{
maint.value += halfChange;
growth.value += halfChange;
}
}
break;
}
case 2:
{
Debug.Log("Slider " + slider + " value has changed to " + growth.value);
if (growth.value == 1)
{
maint.value = 0;
res.value = 0;
}
else
{
if (change > 0)
{
if (maint.value >= halfChange && res.value >= halfChange)
{
res.value -= halfChange;
growth.value -= halfChange;
}
else if (maint.value < halfChange)
{
res.value -= (change - maint.value);
maint.value = 0;
}
else
{
maint.value -= (change - res.value);
res.value = 0;
}
}
else
{
maint.value += halfChange;
res.value += halfChange;
}
}
break;
}
}
}
I'm still learning, so perhaps I don't understand how 'onChangeValue' works? Or are they contradicting each other? I thought about using the event system and the onPointerUp event, but I have no idea how the events work or how to implement that. Any help would be appreciated!
I should add that it looks like my non-user-moved scrollbars are being zero'd out by some kind of loop, but I can't identify it.
Your answer
Follow this Question
Related Questions
How do I find text on an object? 1 Answer
Having an issue blocking UI taps from passing through the UI into a gameobject. 3 Answers
OnPointerEnter works only if enter from blank space. 0 Answers
Alternative to EventSystem.current.currentSelectedGameObject 1 Answer
player jumps too high when clicking with two fingers. 1 Answer