Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

"Object Reference not set to an instance of an object" comes up after attacking an object

Discussion in 'Editor & General Support' started by Baddgers, Mar 20, 2016.

  1. Baddgers

    Baddgers

    Joined:
    Mar 20, 2016
    Posts:
    2
    Hello I am making a defense game based around melee combat. I want the enemy to attack an object, but each time the enemy should actually be hitting the object, the error comes up instead. Here is the code:


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class BruteAttack : MonoBehaviour
    5. {
    6.     private float distance = 1.0f;
    7.     public float timeBetweenAttacks = 0.5f;
    8.     public int attackDamage = 10;
    9.  
    10.  
    11.     Animator anim;
    12.     GameObject player;
    13.     CoreHealth coreHealth;
    14.     EnemyHealth enemyHealth;
    15.     bool playerInRange;
    16.     float timer;
    17.     GameObject brute;
    18.  
    19.     void Awake()
    20.     {
    21.         player = GameObject.Find("Core");
    22.         coreHealth = player.GetComponent<CoreHealth>();
    23.         enemyHealth = GetComponent<EnemyHealth>();
    24.         anim = GetComponent<Animator>();
    25.     }
    26.  
    27.     void OnCollisionEnter(Collision col)
    28.     {
    29.         if (col.gameObject.name == "Core")
    30.         {
    31.             anim.SetTrigger("AttackCore");
    32.             InvokeRepeating("Attack", 0, 1.65f);
    33.         }
    34.     }
    35.  
    36.     void Attack()
    37.     {
    38.         Ray ray = new Ray(transform.position, transform.forward);
    39.         RaycastHit hit;
    40.         if (Physics.Raycast(ray, out hit, distance) && hit.collider.gameObject.tag == "Player" && /*(coreHealth.currentHealth > 0)*/ hit.collider != null)
    41.  
    42.         {
    43.             timer = 0f;
    44.  
    45.             coreHealth.TakeDamage(attackDamage);
    46.                
    47.                 Debug.Log("Brute Hit");
    48.         }
    49.  
    50.         if (coreHealth.currentHealth < 1)
    51.         {
    52.             anim.SetTrigger("CoreDead");
    53.         }
    54.     }
    55. }
     
  2. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    What's the exact error? You didn't even tell which line the error refers to.

    You can also debug the collision when it happens, something like: Debug.Log("Hit: "+col.name);

    You are also checking (hit.collider!=null) after you have referenced to hit.collider in other comparison.
     
  3. Baddgers

    Baddgers

    Joined:
    Mar 20, 2016
    Posts:
    2
    It is referencing coreHealth.TakeDamage(attackDamage);

    The CoreHealth script:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class CoreHealth : MonoBehaviour
    5. {
    6.     private Rigidbody rbody;
    7.     public int startingHealth = 100;
    8.     public GameObject explosion;
    9.     public int currentHealth;
    10.     //public Slider healthSlider;
    11.     //public Image damageImage;
    12.     public AudioClip deathClip;
    13.     public float flashSpeed = 5f;
    14.     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
    15.  
    16.  
    17.     Animator anim;
    18.     AudioSource playerAudio;
    19.     bool isDead;
    20.     bool damaged;
    21.  
    22.  
    23.  
    24.     void Awake()
    25.     {
    26.         anim = GetComponent<Animator>();
    27.         playerAudio = GetComponent<AudioSource>();
    28.         currentHealth = startingHealth;
    29.  
    30.     }
    31.  
    32.     public void TakeDamage(int amount)
    33.     {
    34.         damaged = true;
    35.  
    36.         currentHealth -= amount;
    37.  
    38.         if (currentHealth <= 0 && !isDead)
    39.         {
    40.             Death();
    41.             playerAudio.Play();
    42.             GameObject[] gameObjects = GameObject.FindGameObjectsWithTag("Player");
    43.             foreach (GameObject target in gameObjects)
    44.             {
    45.                 GameObject.Destroy(target);
    46.                 Instantiate(explosion, transform.position, transform.rotation);
    47.             }
    48.         }
    49.     }
    50.  
    51.    
    52.     void Death()
    53.     {
    54.         isDead = true;
    55.  
    56.         Debug.Log("Dead");
    57. }
     
  4. Zaflis

    Zaflis

    Joined:
    May 26, 2014
    Posts:
    438
    So the problem is coreHealth being null. It could happen when you destroy gameobject. You could add these lines at beginning of Attack() in BruteAttack class:
    Code (CSharp):
    1. Debug.Log(coreHealth);
    2. if (!coreHealth) return;