Slider Coding Help Please

Hi everyone, very new to UI Sliders and Js and C# scripting.

I managed to increase and decrease UI Slider when health taken and gained.
But when it comes to increasing maxHealth UI Slider will not match that new maxHealth.

So, how do I go about matching UI Slider with the new maxHealth?
Do I have to make a new function and calling it something like mySliders(I tried but don’t know how to go about)?

Please show example code if possible, will be much appreciated.
My code examle:

if(curHealth < 0 ) {
curHealth = 0;
}

    if(curHealth > maxHealth) {
            curHealth = maxHealth;
    }

    if(Input.GetKeyDown("e")) 
    {
    		healthSlider.value = curHealth;
            curHealth -= 10;
            
    }

What exactly are you trying to achieve?
Do you want to increase the maximum health by a second slider? Or by adjusting a float?
For the float variation:

using UnityEngine;
using UnityEngine.UI;
using SystemCollections;

public class Healthbar : MonoBehaviour {

public Slider healthBar;
public float maxHealth;

void Start{}

void Update{

healthBar.maxValue = maxHealth;
healthBar.minValue = 0;

DecreaseHealth();

}

public void DecreaseHealth ()
{
    if(Input.GetKeyDown("e"))
    {
        healthBar.value -= 10;
    }
}

or, if you want to use a second slider:

    using UnityEngine;
    using UnityEngine.UI;
    using SystemCollections;
    
    public class Healthbar : MonoBehaviour {
    
    public Slider healthBar;
    public Slider maxHealth;
    
    void Start{}
    
    void Update{
    
    healthBar.maxValue = maxHealth.value;
    healthBar.minValue = 0;
    
    DecreaseHealth();
    
    }
    
    public void DecreaseHealth ()
    {
        if(Input.GetKeyDown("e"))
        {
            healthBar.value -= 10;
        }
    }

hope that helps!

You can set the max value for the slider using maxValue variable of the Slider.

Something like in case below the function SetMaxValue() will be where you set the max value for the slider:

SetMaxValue(int newValue)
{
    healthSlider.value = newValue;
}

Now you can call SetMaxValue method from anywhere when you want to change the maxValue for the slider.

In above case if user presses “M” key you can increment the maxHealth by 10.

if(Input.GetKeyDown(KeyCode.M))
{
    maxHealth += 10;
    SetMaxValue(maxHealth);
}

Change the slider so its not equal to the max health but just a percentage representaion of your current health v maxhealth.

healthSlider.value = (curHealth / maxHealth) * 100;

This way yout maxHealth can be any value.