Slider image not updating even though its value does

Hello my fellow Unity developers,

I created a HealthBar object with a slider that has a background image (red) and a filler image (green). Adjusting the slider in the inspector works properly.
I attached a HPBar script to the HealthBar object with the following code:

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

public class HPBar : MonoBehaviour
{
    public Slider slider;

    public void SetCurrentHealth(float hpPercent)
    {
        slider.value = hpPercent;
        Debug.Log(slider.value);
    }
}

The SetCurrentHealth function is called by my CarController script in the following way:

void Update()
{
    HealthBar.GetComponent<HPBar>().SetCurrentHealth(currentHealth / maxHealth);
}

(Okay I probably shouldn’t try to get the HPBar component every update. I will try this tomorrow, but I somewhat doubt that this causes the following problem:)

Even though the debug log constantly spits out the correct value, even when it’s changed during play, the HealthBar image doesn’t change.

How can this be? The value is properly changed, it is properly conveyed to the HPBar script, and adjusting the slider value in the inspector changes the image. But everything together somehow doesn’t work.

Can anyone here please help out a newbie?

Instead of using slider for this, can you add an Image and change it’s type to filled like this?

image
with this, you can directly acces to images fill amount like

[SerializeField] private Image imageReference;

private void UpdateFillBarAmount(float amount)
{
     imageReference.fillAmount = amount;
}

with that you will have much more flexibilty. And it’s how it’s implemented normally. Sliders are for controling slidable areas or so.
Remember this fill amount is between 0 - 1, 0 means empty, 1 means full.