Hey guys
I have a script that all enemies are moving to a box. Now I want that the box can take damage from the enemies and if he dont have any health it should be destroyed. (I already have a Health bar)
thanks,
Dustin
if you want to take damage when an enemy collide with the box, try this:
[SerializeField] private float startHealth;
[SerializeField] private float damageFromEnermy;
private float currentHealth;
// Start is called before the first frame update
void Start()
{
currentHealth = startHealth;
}
private void Update()
{
if (currentHealth <= 0f)
{
Destroy(gameObject);
}
}
private void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.CompareTag("Enermy"))
{
currentHealth -= damageFromEnermy;
// Or if the enermy has its own damage variable:
// currentHealth -= collision.gameObject.GetComponent<EnermyScript>().damage;
}
}
If it doesn’t work it could be because you put compare tag to enermy instead of enemy.