UI slider value control

Hi.

i made a car game that can get some bonus like Nitro and i have a slider in my scene that show Nitro time. (default slider value is 5.)

so i have NitroTimer in my code that i can decrease my slider value with it. like below:

if (NitroTimer > 0)
{
	NitroTimer -= Time.deltaTime;
	slider.value = NitroTimer;
}

All things work very well. but i want when NitroTimer <= 0 and my slider value is 0, Simultaneously
i increase slider value to 5 again and decrease NitroSpeed to 0.
for do this i used this code but it don’t work correctly:

if (NitroTimer <= 0 && NitroSpeed > TempSpeed)  // TempSpeed is last car speed before enable NitroSpeed 
{
	NitroSpeed -= 1f;  // decrease NitroSpeed 

    // increase slider value
	float val = NitroSpeed / 5 * Time.deltaTime;   
	slider.value += val;
}

Does anyone how can do it?

Thanks.

Hello, So I made a short example script below:

using UnityEngine;
using UnityEngine.UI;

public class Nitro : MonoBehaviour
{
    //Actual Nitro
    private float nitro = 5;

    //Attach slider in the editor
    public Slider nitroSlider;

    private void Update()
    {
        //If space is pressed and we have nitro
        if (nitro > 0 && Input.GetKeyDown(KeyCode.Space))
        {
            nitro -= Time.deltaTime;
        }
        else //Else refil over time
        {
            nitro += Time.deltaTime;
        }

        //Clamp it between 0 and 5
        nitro = Mathf.Clamp(nitro, 0, 5);
        nitroSlider.value = nitro;
    }
}

Note: If you need anything explained please ask.

Update

I saw that you were looking at lerping, so I decided to have some fun and update the above example:
using System;
using UnityEngine;
using UnityEngine.UI;

public class Nitro : MonoBehaviour
{
    [Header("Lerp the Value or Clamp it.")]
    [SerializeField]
    bool lerp = true;

    //Actual Nitro value, start is 0
    private float nitro = 0;

    //LERP

    //Show in the inspector
    [SerializeField]
    float nitroMin = 0;
    [SerializeField]
    float nitroMax = 5;

    [Range(0,1)]
    [SerializeField]
    float nitroSpeedRegen = 0.1f;
    float lerpSpeed;

    // /LERP

    //Attach slider in the editor
    public Slider nitroSlider;

    private void Update()
    {
        if (lerp && !Input.GetKey(KeyCode.Space))
        {
            LerpNitro();
        }
        else if (!Input.GetKey(KeyCode.Space))
        {
            ClampNitro();
        }

        //Check if we are using nitro
        activateNitro();
    }

    private void activateNitro()
    {
        //If space is pressed and we have nitro
        if (nitro > 0 && Input.GetKey(KeyCode.Space))
        {
            //Decrease Nitro
            nitro -= Time.deltaTime;
            //Update the slider
            nitroSlider.value= nitro;
            //Work out new lerp speed
            lerpSpeed = nitro/nitroMax;
        }
    }

    private void ClampNitro()
    {
        //Clamp it between 0 and 5
        nitro = Mathf.Clamp(nitro, 0, 5);
        nitroSlider.value = nitro;
    }

    void LerpNitro()
    {
        //Calculations
        lerpSpeed += nitroSpeedRegen * Time.deltaTime;
        nitro = Mathf.Lerp(nitroMin, nitroMax, lerpSpeed);

        //Apply the value to the slider
        nitroSlider.value= nitro;

        //Prevent the speed from going too fast
        Mathf.Clamp(lerpSpeed, 0, 1);
    }
}

Note: the above includes both the clamping and the lerping. Attach this script to any gameobject and assign the slider.

Thereafter simply pressing space will decrease the value, and not doing anything will increase the value. Yet again, if you have any questions please ask

Some night time reading: lerping, Clamping, SerializeField if you want to know what these functions do.