-1

My HealthBar script:

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

public class HealthBar : MonoBehaviour
{

    public Slider slider;
    public Gradient gradient;
    public Image fill;

    public void SetMaxHealth(int health)
    {
        slider.maxValue = health;
        slider.value = health;

        fill.color = gradient.Evaluate(1f);
    }

    public void SetHealth(int health)
    {
        slider.value = health;

        fill.color = gradient.Evaluate(slider.normalizedValue);
    }

}

My Player script:

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

public class Player : MonoBehaviour
{
    public int maxHealth = 100;
    public int currentHealth;

    public HealthBar healthBar;

    private void Start()
    {
       currentHealth = maxHealth;
       healthBar.SetHealth(maxHealth);
    }

    private void Update()
    {
       if (currentHealth == 50)
       {
           healthBar.SetHealth(currentHealth);
       }

       if (currentHealth == 0)
       {
           Die();
        }
     }

     private void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.CompareTag("Enemy"))
         {
             TakeDamage(50);
         }
     }

     void TakeDamage(int damage)
     {
         currentHealth -= damage;
     }

    void Die()
    {
       Destroy(gameObject);
       LevelManager.instance.Respawn();

       healthBar.SetHealth(maxHealth);
     }
 }

My LevelManager script:

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

public class LevelManager : MonoBehaviour {
    public static LevelManager instance;

    public Transform respawnPoint;
    public GameObject playerPrefab;

    public CinemachineVirtualCameraBase cam;

    [Header("Currency")]
    public int currency = 0;

    private void Awake() {
       instance = this;
    }

    public void Respawn() {
       GameObject player = Instantiate(playerPrefab, 
       respawnPoint.position, Quaternion.identity);
       cam.Follow = player.transform;
    }

    public void IncreaseCurrency(int amount) {
       currency += amount;
    }
}

The errors that I am getting:

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

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

NullReferenceException: Object reference not set to an instance of an object
Player.Die () (at Assets/Scripts/Player.cs:49)
Player.Update () (at Assets/Scripts/Player.cs:27)

I have been trying to figure the problem out for a while. It works, when I die once. unfortunately, when I try die again,it doesn't work . The error message pops up, when I collide with the enemy.

How do I fix these errors pls help.

2 Answers2

1

Is the healthBar being destroyed on death? It makes sense since we want to make the health bar disappear when the player dies but make sure to instantiate it again on respawn.

JaredP
  • 19
  • 2
0

You should assign a HealthBar component to PlayerScript's healthBar property from inspector.