2D Area of Effect Script

Hi, this is my first attempt at an AoE script.
I attached the following to my rockets to do damage and apply an explosion force effect to the enemy it hits and any other enemies within a certain radius:

using UnityEngine;
using System.Collections;

public class RocketExplosion : MonoBehaviour {

public float expRadius = 10f;			// Radius within which enemies are damaged.
public float expForce = 100f;			// Force that enemies are thrown from the blast.
public GameObject soundPrefab;			// Audioclip of explosion.
public GameObject explosionPrefab;		// Prefab of explosion effect.
public float Damage=100;



void Start ()
{
	

}
	
void OnTriggerEnter2D(Collider2D other){
	if(other.gameObject.tag == "Enemy"){

	
	
	// Find all the colliders on the Enemies layer within the expRadius.
	Collider2D[] enemies = Physics2D.OverlapCircleAll (transform.position, expRadius, 1 << LayerMask.NameToLayer("EnemyLayer"));
	
	// For each collider...
	foreach(Collider2D en in enemies)
	{
		// Check if it has a rigidbody (since there is only one per enemy, on the parent).
		Rigidbody2D rb = en.GetComponent<Rigidbody2D>();
		if(rb != null && rb.tag == "Enemy")
		{
			// Find the Enemy script and apply damage.
			rb.gameObject.GetComponent<HealthEnemy>();
			HealthEnemy hp=(HealthEnemy)transform.GetComponent("HealthEnemy"); 
			if(hp){
				if(hp.CurrentHealth<=hp.MaxHealth){		
					hp.CurrentHealth=hp.CurrentHealth-Damage;
					
				}
			}
			
			// Find a vector from the rocket to the enemy.
			Vector3 deltaPos = rb.transform.position - transform.position;
			
			// Apply a force in this direction with a magnitude of expForce.
			Vector3 force = deltaPos.normalized * expForce;
			rb.AddForce(force);
		}
	}
}

	

	// Instantiate the explosion prefab.
	Instantiate(explosionPrefab,transform.position, Quaternion.identity);
	
	// Play the explosion sound effect.
	Instantiate(soundPrefab, transform.position,transform.rotation);
	


}

}

Now the explosion is not going off half the time, and it doesn’t cause damage to the enemies.
The explosion force effect seems to work most of the time though.
What am I doing wrong?

Well the problem with not applying damage to enemies is from this:

rb.gameObject.GetComponent<HealthEnemy>();
HealthEnemy hp=(HealthEnemy)transform.GetComponent("HealthEnemy");

The first line is doing nothing but wasting time. You’re retrieving the HealthEnemy script, but not storing the reference or doing anything else with it. The second line is checking for a component by name on the same object as the script (the rocket in this case) because you’re using ‘transform’. Both these lines should be replaced by this:

HealthEnemy hp = rb.GetComponent<HealthEnemy>();

There’s no need to go through ‘gameObject’ as you can search for a component from other component references.

Also, should the AoE only apply if the missile hits an enemy directly? The tag check at the start of OnTriggerEnter2D() is wrapping the entire damage/force section, so if the rocket hits something other than an enemy, no damage or force would be applied even if one was within range.

As for the explosion effect not going off, I’m not sure. The Instantiate()s should be working every time it triggers. I’d suggest adding a Debug.Log to the start of the OnTriggerEnter2D() to see if it’s even triggering when the effect doesn’t happen.

Thank You kindly OncaLupe.
This works perfectly now,
i didn’t even think about the tag issue, but yes, I do want the AoE damage if i hit walls etc.
The issue with the explosion not triggering was fixed by making the collider on the rocket a bit bigger.