Question by
unity_T2PcF_gS6-wL-g · Jun 20, 2019 at 06:54 AM ·
collisioncollidershealthbarhealth-deduction
Need a simple health script
Need a script to attach to my player where every collision occurs with the player-object its heath decreases by 1 out of 10 and when it reaches 0 the player is destroyed(i.e. dies). I'm having a problem with OnCollision method so a small workable script would be enough. Thank You.
Comment
Answer by aoun111 · Jul 08, 2019 at 09:28 AM
using UnityEngine;
public class Player : MonoBehaviour {
public float health = 10f;
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.gameObject.tag == "SetTagOfObjectHereAndInEditor")
{
health -= 1f;
}
}
void Update() {
if (health <= 0f)
{
// Player is dead. Delete him here etc...
}
}
}