An error occurred while creating Slider Health and shield

I am now learning how to make the game “Space Shooter”, I strictly observe how the script is written in the video tutorial (that is, I copy it). I got an error, although the author has nothing of the kind (it is impossible to ask the author). The error is related to the slider. The error comes out on line 43, then 66 and 82.

NullReferenceException: Object reference not set to an instance of an object
Player.Start () (at Assets/Scripts/Player.cs:43)

NullReferenceException: Object reference not set to an instance of an object
Player.GetDamageShield (System.Int32 damage) (at Assets/Scripts/Player.cs:66)
Enemy.OnTriggerEnter2D (UnityEngine.Collider2D coll) (at Assets/Scripts/Enemy.cs:84)

NullReferenceException: Object reference not set to an instance of an object
Player.GetDamage (System.Int32 damage) (at Assets/Scripts/Player.cs:82)
Enemy.OnTriggerEnter2D (UnityEngine.Collider2D coll) (at Assets/Scripts/Enemy.cs:78)

9195986–1281995–Player.cs (3.05 KB)

// Look for an object with tag hp_Player and take the Slider component from it
_slider_hp_Player = GameObject.FindGameObjectWithTag("sl_HP").GetComponent<Slider>();
// Look for an object with tag hp_Shield and take the Slider component from it
_slider_hp_Shield = GameObject.FindGameObjectWithTag("sl_Shield").GetComponent<Slider>();

I’d say it is a terrible tutorial but it is what it is.
Since you didn’t get a null reference in Awake I can assume that GameObject.FindGameObjectWithTag() probably did find a game object matching with that tag.
However, GetComponent<Slider>() probably returns null because there is no Slider component on that game object.

Since you’re a beginner, I’d suggest changing the 2 variables from private to public.

// Reference private to the UI's health bar.
    private Slider _slider_hp_Player;
    // Reference private to the UI's shield bar.
    private Slider _slider_hp_Shield;

Then drag drop a reference from the scene into the components fields in the inspector.
Then remove the 2 lines that find a game object & get component.
You also might want to change the name of the variable because it is then no longer private.
public fields are usually PascalCase.
private fields either with a _ prefix or just lowercase. (which sometimes differs from dev to dev)

The first rule of GameObject.Find and relatives is to not use it at all.

Instead of attaching the script to the thread post, use code tags instead

Do not make duplicate posts, especially for things that don’t require posts at all.

The answer is always the same… ALWAYS!

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that

@Kurt-Dekker Sorry, i’m first time in this site

I found the problem. The sl_HP and sl_Shield tags should have been placed on the Sliders itself, not on the hp bar objects.