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)