I have an object with children game objects that are the compound collider. The Parent gameobject has the EnemyHealth script attached. I can collide with the compound colliders, and when my ‘lasers’ collide with the parent gameobject they spawn the explosion but do not reference the EnemyHealth script.
Is this because the on Trigger into is trying to get the EnemyHealth from the compound collider and not from the Parent gameobject with EnemyHeath attached to it?
What’s the correct way of trying to find the parents EnemyHealth component then?
void OnTriggerEnter (Collider other)
{
if (other.tag == "Enemy")
{
EnemyHealth enemyHealth = other.GetComponent<Collider>().GetComponent <EnemyHealth>();
if(enemyHealth != null)
{
enemyHealth.TakeDamage (attackDamage);
}
TrashMan.spawn (explosion, transform.position, Quaternion.Euler(270, 0, 0));
//Despawn laser
TrashMan.despawn (gameObject);
}
}