I am fairly new to Unity and can’t seem to figure this out. I have the “EnemyLife.cs” script attached to my enemy. It has a DecreaseHealth method:
public void DecreaseHealth(float damage)
{
hp -= damage;
}
Now, when a project tile hits the enemy, I want it to call this method and decrease the health by the amount of damage of the projectile.
void OnTriggerEnter(Collider other)
{
if (other.tag == "Enemy")
{
enemy = other.transform;
EnemyLife enemyLife = other.transform.GetComponent<EnemyLife>();
enemyLife.DecreaseHealth(damage);
Destroy(gameObject);
}
}
I’ve tried a few variations of this such as:
EnemyLife enemyLife = other.transform.GetComponent("EnemyLife") as EnemyLife;
enemyLife.DecreaseHealth(damage);
And even just using SendMessage(“DecreaseHealth”, damage) and none of it seems to work. I feel like there is something fundamental that I am missing.