Calling a method from a separate script on trigger

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.

Your code looks right.

Double check that it is actually hitting the enemy by throwing in a Debug.Log

you could also do

if(enemyLife==null)Debug.Log("didn't get component");

after you GetComponent.

Also double check that the target has the “Enemy” tag.

Change

EnemyLife enemyLife = other.transform.GetComponent<EnemyLife>();

to:

EnemyLife enemyLife = other.GetComponent<EnemyLife>();