Jak w temacie projektuje pasek życia i mam błąd: NullReferenceException: Object reference not set to an instance of an object
to mój skrypt na pasek
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealthUserI : MonoBehaviour {
public Image HealthU;
private PlayerHealth h;
void Start()
{
h = GetComponent<PlayerHealth>();
}
void Update ()
{
healthUser();
}
private void healthUser()
{
HealthU.fillAmount = h.health / 100f;
}
}
oraz skrypt do którego się odwołuję get componentem
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour {
public float health = 100f;
public GameObject deadFX;
public void TakeDamage(float damageAmount)
{
health -= damageAmount;
if(health <= 0)
{
Instantiate(deadFX, transform.position, Quaternion.identity);
Destroy(gameObject);
}
}
}
Czemu ten błąd wyskakuje? Wydaje mi się, że tu wszystko jest dobrze.