NullReferenceException: Object reference not set to an instance of an object

Hello, I was messing around with a slider when suddenly I got an error that I could not fix the error was NullReferenceException: Object reference not set to an instance of an object HealthBar.Start () (at Assets/Scripts/UI/HealthBar.cs:12)

I looked at the line of the error but I could not find the problem of it

code:

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

public class HealthBar : MonoBehaviour {

    private Slider slider;
    private float thing;

	void Start () {
        slider = GetComponent<Slider>();
        thing = slider.value;
	}
	
	void Update () {
        slider.value = 10;
	}

    public void damage(float args)
    {
        thing -= args;
    }

    public void heal(float args)
    {
        thing += args;
    }

    public void set(float args)
    {
        thing = args;
    }

    public float get()
    {
        return thing;
    }
}

as you can see I am adding the slider object on line 12 but still it gives me a nullpointer can someone help me with this problem?

sorry for bad english

EDIT:
Debugged slider and returned null even when i manualy added slider object

You’re not adding a slider component on line 12 - you’re trying to access an existing Slider component attached to this Gameobject. And the null error is occurring because it hasn’t found one.

Either add a Slider to the Gameobject to which this script is attached manually, or else change GetComponent to AddComponent.