using UnityEngine; using UnityEngine.SceneManagement; public class HealthBar2D : MonoBehaviour { public int life = 5; public Transform healthBar; float startLife; float localX; void Start() { startLife = life; localX = healthBar.localScale.x; } void OnTriggerEnter2D(Collider2D other) { if (other.tag == "Player") { life--; healthBar.localScale = new Vector3(localX * life / startLife, healthBar.localScale.y, 1f); if (life <= 0) { Destroy(gameObject); } } } }