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.
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