If enemy collides, lose health?

Hi,

I’m new to Unity, I’ve been learning for about a month, and I’m making a platformer from tutorials from YouTube. However, I can’t seem to find tutorials, as the subject implies, if the enemy collides with the player, you lose health from the health bar.

I’m learning Unity for my coursework, and by learning, I mean looking up tutorials from YouTube.

I’ve already got a Health Bar, a player with scripts for platforming and that jazz, platforms, and a enemy. The Health Bar is from:

however I’ve not done the coding because I don’t think they’ve gotten a player or an enemy to test it out. I’ve got the platformer scripts from Brackeyes and the enemy AI too.

Any help would be greatly appreciated.

Thanks.

Changed the health bar to a slider. Still don’t know what to do…

I suggest checking out some other tutorials on that topic. For example maybe this will help:

hi I can help you I’m studying unity just like you. This can be done using the slider.
You need in canvas make ui slider, in the handle slide area delete handle, fill area you can make red. Then turn to the slider and make max value 100(it will be max hp, you can make yours numbers). Then make HealthBar script in your player.
In this script write:
public float health;
public Slider slider;
public GameObject deathEffect;
//means effect of die it can be animation or any prifabe or emptyObject

void Start()
{
slider.value =health;
}

public void TakeDamage(float damage){
health -=damge;

if(health <= 0f){
Die();
}

void Die()
{
Instantiate(deathEffect, transform.position,Quaternion.identity);
Destroy(gameObject);
}

}

In the enemy script you need to make collider that control have player touch it or not. I hope you did it, but if you don’t make it, tell me. And in the enemy script make public int damge = 20; for example
To call function of damge to the player make

If(/write here if player touch collider/){
Healthbar health = transform.GetComponent();//there can be a problem with the script
//Healthbar is name of last sript
if (health != null){
health.TakeDamage(damge);
//TakeDamage it is a void in HealthBar script
}
}

It is all
Please contact me in instagram, l can help if something go wrong
@svyathor

Sorry for my English