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

I am on Character Stats > Enemy Health Display lecture in the RPG Combat Course.

I am getting NullReferenceException: Object reference not set to an instance of an object with displaying the Percentage text for the Enemy Health Value.

I double checked the code fro Enemy Health Display and Fighter.cs but cannot get why the Percentage is not happening.

If I remove health.GetPercentage() from
GetComponent().text = String.Format("{0:0}%", health.GetPercentage());
it works, but when I add it I get the Null Reference. Here is the complete code

Enemy Health Display

namespace RPG.Combat

public class EnemyHealthDisplay : MonoBehaviour
{
    Fighter fighter;

    private void Awake()
    {
        fighter = GameObject.FindWithTag("Player").GetComponent<Fighter>();
    }

    private void Update()
    {
        if (fighter.GetTarget() == null)
        {
            GetComponent<Text>().text = "N/A";
        }
        
        Health health = fighter.GetTarget();
        GetComponent<Text>().text = String.Format("{0:0}%", health.GetPercentage());
    }
}

Fighter

namespace RPG.Combat

public class Fighter : MonoBehaviour, IAction, ISaveable
{
    [SerializeField] float timeBetweenAttacks = 1f;
    [SerializeField] Transform rightHandTransform = null;
    [SerializeField] Transform leftHandTransform = null;
    [SerializeField] Weapon defaultWeapon = null;
    Health target;
    float timeSinceLastAttack = Mathf.Infinity;
    Weapon currentWeapon = null;
    private void Start() 
    {
        if (currentWeapon == null)
        {
            EquipWeapon(defaultWeapon);
        }
    }
    private void Update()
    {
        timeSinceLastAttack += Time.deltaTime;
        if (target == null) return;
        if (target.IsDead()) return;
        if (!GetIsInRange())
        {
            GetComponent<Mover>().MoveTo(target.transform.position, 1f);
        }
        else
        {
            GetComponent<Mover>().Cancel();
            AttackBehaviour();
        }
    }
    
    public void EquipWeapon(Weapon weapon)
    {
        currentWeapon = weapon;
        Animator animator = GetComponent<Animator>();
        weapon.Spawn(rightHandTransform, leftHandTransform, animator);
    }
    
    public Health GetTarget()
    {
        return target;
    }
    
    private void AttackBehaviour()
    {
        transform.LookAt(target.transform);
        if (timeSinceLastAttack > timeBetweenAttacks)
        {
            // This will trigger the Hit() event.
            TriggerAttack();
            timeSinceLastAttack = 0;
        }
    }
    private void TriggerAttack()
    {
        GetComponent<Animator>().ResetTrigger("stopAttack");
        GetComponent<Animator>().SetTrigger("attack");
    }
    // Animation Event
    void Hit()
    {
        if(target == null) { return; }
        
        if (currentWeapon.HasProjectile())
        {
            currentWeapon.LaunchProjectile(rightHandTransform, leftHandTransform, target);
        }
        else
        {
            target.TakeDamage(currentWeapon.GetDamage());
        }
    }
    void Shoot()
    {
        Hit();
    }
    private bool GetIsInRange()
    {
        return Vector3.Distance(transform.position, target.transform.position) < currentWeapon.GetRange();
    }
    public bool CanAttack(GameObject combatTarget)
    {
        if (combatTarget == null) { return false; }
        Health targetToTest = combatTarget.GetComponent<Health>();
        return targetToTest != null && !targetToTest.IsDead();
    }
    public void Attack(GameObject combatTarget)
    {
        GetComponent<ActionScheduler>().StartAction(this);
        target = combatTarget.GetComponent<Health>();
    }
    public void Cancel()
    {
        StopAttack();
        target = null;
        GetComponent<Mover>().Cancel();
    }
    private void StopAttack()
    {
        GetComponent<Animator>().ResetTrigger("attack");
        GetComponent<Animator>().SetTrigger("stopAttack");
    }
    public object CaptureState()
    {
        return currentWeapon.name;
    }
    public void RestoreState(object state)
    {
        string weaponName = (string)state;
        Weapon weapon = UnityEngine.Resources.Load<Weapon>(weaponName);
        EquipWeapon(weapon);
    }
}

}

some help is needed.

Never mind, we fix it in the next video

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms