Help with Getting Slider to fill over time

Hi there, I’ve been trying to Google and figure this out for hours and all the implementations I’ve found online aren’t working. I don’t have a reload animation currently (but at this point that probably would’ve been faster) and wanted to just have a slider fill over the time it took to reload to have a visual representation. The problem being that I can’t get a slider to fill over 3 seconds after I press reload for the life of me.

Here’s what I currently have:

using System.Collections;
using System.Collections.Generic;
using UnityEditor.PackageManager.Requests;
using UnityEngine;
using UnityEngine.UI;

[RequireComponent(typeof(Slider))]
public class SliderTimer : MonoBehaviour
{
    Slider _slider;

    private void Start()
    {
        _slider = GetComponent<Slider>();
        _slider.minValue = 0f;
        _slider.maxValue = 3f;
    }

    private void Update()
    {
        CheckForReload();

        if (CheckForReload())
        {
            FillSlider();
        }
        else
        {
            ResetSlider();
        }
    }

    bool CheckForReload()
    {
        GameObject gunObject = GameObject.Find("WPN_Eder22");
        GunScript gun = gunObject.GetComponent<GunScript>();

        return gun.reloading;
    }

    void FillSlider()
    {
        _slider.value = Mathf.Lerp(_slider.minValue, _slider.maxValue, Time.deltaTime * 1f);
    }

    void ResetSlider()
    {
        _slider.value = _slider.minValue;
    }
}

Time.deltaTime, does not function whatsoever, and Time.time also doesn’t seem to function properly. I feel like this is a lot more simple than I am making it out to be but I’d really appreciate it if someone could help me get on track with this.

Thanks!

EDIT: Well this is annoying, the second I make a post online is when I solve it.

Solution was to take this part:

_slider.value = Mathf.Lerp(_slider.minValue, _slider.maxValue, Time.deltaTime * 1f);

Add a float called “fillTime” set to 0f.

Then change the lines to this:

_slider.value = Mathf.Lerp(_slider.minValue, _slider.maxValue, fillTime);

fillTime += 0.375f * Time.deltaTime;
1 Like

Yes, that is an excellent solution. Time.time isn’t good because it is the time from the launch of the game.

By selecting your own variable fillTime you can control exactly everything about it, when it resets, how it goes up, etc., as you are doing.

And using Time.deltaTime to count it up, that way is precisely what it is for.