i cant solve this some one help me

hello im new in the scripting thing but i was hoping u can help me i build a ui system that keep track of the health and the items that the player has right now but i allways get the error
and the ui is not changing to the health or the items what to do?:

NullReferenceException: Object reference not set to an instance of an object
UI.Start () (at Assets/UI.cs:17)

oh and u can download the codes below if u want

this is the code:

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

public class UI : MonoBehaviour
{
public Text Life;
public Text Bullets;

private PlayerManager player;
private float health;
private int item;

public void Start()
{
health = player.health;
item = player.itemCount;
}

// Update is called once per frame
private void Update()
{
Life.text = health.ToString();
Bullets.text = $“{item.ToString()}/3”;
}
}

this is the playmanager code:

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

public class PlayerManager : MonoBehaviour
{
public int id;
public string username;
public float health;
public float maxHealth;
public int itemCount = 0;
public MeshRenderer model;

public void Initialized(int _id, string _username)
{
id = _id;
username = _username;
health = maxHealth;
}

public void SetHealth(float _health)
{
health = _health;

if (health <= 0f)
{
Die();
}
}

public void Die()
{
model.enabled = false;
}

public void Respawn()
{
model.enabled = true;
SetHealth(maxHealth);
}
}

6890231–805724–UI.cs (564 Bytes)
6890231–805727–PlayerManager.cs (810 Bytes)

First, use code tags, they provide better formatting and line numbers. Second, null errors as easy to fix. Something doesn’t have a value. So you can go to the line it references (line 17 in your errors case) and figure out what is null. I’m not going to count the lines, but at a glance, my guess is since you have a private PlayerManager variable, chances are when you try to get health and itemCount from it, the variable player is null.

So you have to assign a value to player. Simple approach is just make the variable Public and drag and drop in the inspector.

i need to make player public ?

oh an btw this is line 17 : health = player.health;
but i get the same in line 18 to:item = player.itemCount

As I said, the simple approach is to make player public and drag and drop your PlayerManager gameObject into the slot, just like you hopefully did with your two Text objects.

Of course you will, because player is null. Null errors are the simplest to solve, and fairly common, so get use to seeing them and learning how to solve them.

TNX its work but i got new problem now the ui isnt showing the numbers its need its stuck on zero and i dont get any errors

never mind now i fixed it all tnx anyway