Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Object reference not set to an instance of an object error in my script

Discussion in 'Scripting' started by SenseiWill, Aug 20, 2020.

  1. SenseiWill

    SenseiWill

    Joined:
    Aug 20, 2020
    Posts:
    2
    Being told "NullReferenceException; Object reference not set to an instance of an object" on line 24?
    Really new so don't be harsh if its obvious but any clue what i've done wrong?

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Diagnostics;
    5. using UnityEngine;
    6.  
    7. public class PlayerCombat : MonoBehaviour
    8. {
    9.     public int maxHealth = 5;
    10.     public int currentHealth;
    11.     public HealthBar healthBar;
    12.  
    13.     public Animator animator;
    14.  
    15.     public Transform AttackPoint;
    16.     public LayerMask enemyLayers;
    17.  
    18.     public float attackRange = 0.5f;
    19.     public int attackDamage = 2;
    20.  
    21.  
    22.     void Start()
    23.     {
    24.         currentHealth = maxHealth;
    25.         healthBar.SetMaxHealth(maxHealth);
    26.     }
    27.  
    28.     // Update is called once per frame
    29.     void Update()
    30.     {
    31.      
    32.         if (Input.GetMouseButtonDown(0))
    33.         {
    34.             Attack();
    35.         }
    36.     }
    37.  
    38.     void Attack()
    39.     {
    40.         // play animation
    41.         animator.SetTrigger("Attack");
    42.  
    43.         // Detect enemies in range of attack
    44.         Collider2D[] hitEnemies = Physics2D.OverlapCircleAll(AttackPoint.position, attackRange, enemyLayers);
    45.  
    46.         // Damage
    47.         foreach(Collider2D enemy in hitEnemies)
    48.         {
    49.             enemy.GetComponent<Enemy>().TakeDamage(attackDamage);
    50.         }
    51.     }
    52.  
    53.     void OnDrawGizmosSelected()
    54.     {
    55.         if (AttackPoint == null)
    56.             return;
    57.  
    58.         Gizmos.DrawWireSphere(AttackPoint.position, attackRange);
    59.     }
    60.  
    61.     void hit()
    62.     {
    63.  
    64.     }
    65.  
    66.     void TakeDamage(int damage)
    67.     {
    68.         currentHealth -= damage;
    69.  
    70.         healthBar.SetHealth(currentHealth);
    71.  
    72.         if (currentHealth <= 0)
    73.         {
    74.             Die();
    75.         }
    76.  
    77.     }
    78.  
    79.     void Die()
    80.     {
    81.         UnityEngine.Debug.Log("You Died");
    82.  
    83.         // die animation
    84.  
    85.         // disable self
    86.     }
    87.  
    88. }
    89.  
     
  2. SenseiWill

    SenseiWill

    Joined:
    Aug 20, 2020
    Posts:
    2
    Line 25 on this post coz it uploaded weird. Something about my healthBar
     
  3. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,556
    If HealthBar class is a MonoBehaviour, first select the object with the PlayerCombat component in your scene, then look in the Inspector to make that you have something assigned to "Health Bar" (and drag your health bar to it, if things are set up that way) - otherwise you'll have to look wherever it's being assigned procedurally, i.e. search for
    .healthBar =
     
    SenseiWill likes this.