I need help with projectile taking enemy health c# script.
1 Answer
1You need to access the enemy’s script, then access two public functions within that script. One to tell the enemy to take damage and one to get the current health of the enemy.
void OnTriggerEnter(Collider otherObject)
{
if(otherObject.tag == "Enemy")
{
// EnemyScript is attached to the enemy
EnemyScript e = otherObject.gameObject.GetComponent<EnemyScript>();
// Create a public function in the EnemyScript to tell it to take damage and pass the damage
e.TakeDamage(10f);
// Make a public function in EnemyScript to return the health of the enemy
if ( e.GetHealth() <= 0f)
{
Destroy(otherObject.gameObject);
}
}
}
Here are is the GetHealth() and TakeDamage() functions just in case.
public float GetHealth()
{
return currentHealth;
}
public void TakeDamage(float damage)
{
currentHealth -= damage;
}
Hope this helps.
If you are having problems with your script, edit your question and post it here.
– aldonaletto