Grenade damage script error

i will grenade damage to enemies but when i write this code and throw the grenade i get this error. how can i fix this?

NullReferenceException: Object reference not set to an instance of an object ApplyDamage.Attack () (at Assets/ApplyDamage.cs:33)

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ApplyDamage : MonoBehaviour {
     int damage = 100;
     private GameObject[] enemy;
     EnemyHealth enemyHealth;
     private bool enemyInRange;
     // Use this for initialization
     void Start () {
         GameObject[] enemy = GameObject.FindGameObjectsWithTag ("Enemy");
         foreach(GameObject enemies in enemy)
         {
             enemyHealth = enemies.GetComponent<EnemyHealth> ();
         }
     }
     void OnTriggerEnter (Collider other)
     {
             enemyInRange = true;
     }
     void OnTriggerExit (Collider other)
     {
             enemyInRange = false;
     }
     // Update is called once per frame
     void Update () {
         if (enemyInRange) {
             Attack ();
         }
     }
     public void Attack()
     {
         enemyHealth.TakeDamage (damage);
     }
}

It means “enemyHealth” is still null because your “enemies.GetComponent()” does not find such a component.

Also, it seems you’re iterating through enemies and only storing the last EnemyHealth component, even though I guess you were supposed to gather all EnemyHealth components and apply the damage to all of them. You could use a list for that:

private List<EnemyHealth> enemyHealths = new List<EnemyHealth>();

void Start()
{
    GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");
    foreach (GameObject enemy in enemies)
    {
        enemyHealths.Add(enemy.GetComponent<EnemyHealth>());
    }
}

public void Attack()
{
    foreach (EnemyHealth health in enemyHealths)
    {
        health.TakeDamage(damage);
    }
}

Just ignore this code, if this wasn’t what you intended to do.

1 Like

it doesn’t work. same error. So this is actual code. when explosion the grenade, the enemy will die but i can’t.

//Explosion force
        Vector3 explosionPos = transform.position;
        Collider[] colliders = Physics.OverlapSphere (explosionPos, radius);
        foreach (Collider hit in colliders) {
            Rigidbody rb = hit.GetComponent<Rigidbody> ();

            //Add force to nearby rigidbodies
            if (rb != null)
                rb.AddExplosionForce (power, explosionPos, radius, 3.0F);
           
            }

            //Destroy the grenade on explosion
            Destroy (gameObject);
        }

Do all of this in ontriggerenter():

If the other.collider.tag is = to the enemy tag, do other.gameobject.GetComponent().takeDamage(damage)

2 reasons yours isnt working, your taking damage if the trigger hits any objects, and your not taking damage from the enemy it hit only, the enemy which the script belongs too

i shared actual code above. i must do in this.