Balance UI Sliders

Hi! I have 4 UI sliders whose values I want to sum equal to or less than 100. I’ve been trying to figure this out by myself for a while, but I’m still very new to this, so I’m having a bit of trouble. Any help would be appreciated :slight_smile: Here is what I currently have:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class StatBalance : MonoBehaviour
{
	private float[] set;
	public float sliderValue1 = 0f;
	public float sliderValue2 = 0f;
	public float sliderValue3 = 0f;
	public float sliderValue4 = 0f;

	void Start()
	{
		set = new float[4];
		set[0] = sliderValue1;
		set[1] = sliderValue2;
		set[2] = sliderValue3;
		set[3] = sliderValue4;
	}

	void AdjustValues()
	{    
		// balance set of values here
	}


		/// Note: these sliders have a range of 0f to 100f
	public void SliderValue1(float uiSliderValue1)
    {
        sliderValue1 = uiSliderValue1;
    }

	public void SliderValue2(float uiSliderValue2)
    {
        sliderValue2 = uiSliderValue2;
    }

	public void SliderValue3(float uiSliderValue3)
    {
        sliderValue3 = uiSliderValue3;
    }

	public void SliderValue4(float uiSliderValue4)
    {
        sliderValue4 = uiSliderValue4;
    }
}

using System;
using UnityEngine;
using UnityEngine.UI;

public class StatBalance : MonoBehaviour
{
    [SerializeField] private Slider[] sliders;
    private float[] values;

    // Start is called before the first frame update
    void Start()
    {
        if (sliders.Length > 1)
        {
            for (int sliderIndex = 0 ; sliderIndex < sliders.Length ; sliderIndex++)
            {
                int i = sliderIndex;
                Slider slider = sliders[sliderIndex];
                SetRatio(slider, 1f / sliders.Length);
                slider.onValueChanged.AddListener(_ => BalanceSliders(i));
            }
            values = Array.ConvertAll(sliders, GetRatio);
        }
    }


    private void BalanceSliders(int updatedSliderIndex)
    {
        Slider updatedSlider = sliders[updatedSliderIndex];

        for (int sliderIndex = 0 ; sliderIndex < sliders.Length ; sliderIndex++)
        {
            if (sliderIndex != updatedSliderIndex)
            {
                Slider slider = sliders[sliderIndex];

                float ratio = values[updatedSliderIndex] >= 1f - Mathf.Epsilon
                    ? 0
                    : values[sliderIndex] / (1 - values[updatedSliderIndex]);
                float value = (1 - GetRatio(updatedSlider)) * ratio;

                SetRatio(slider, value);
            }
        }

        for (int sliderIndex = 0 ; sliderIndex < sliders.Length ; sliderIndex++)
        {
            values[sliderIndex] = GetRatio(sliders[sliderIndex]);
        }
    }

    private float GetRatio(Slider slider)
    {
        return Mathf.InverseLerp(slider.minValue, slider.maxValue, slider.value);
    }

    private void SetRatio(Slider slider, float ratio)
    {
        slider.SetValueWithoutNotify(Mathf.Lerp(slider.minValue, slider.maxValue, ratio));
    }
}