Does anyone know how to make the health bar stop at zero? Sometimes the player dies but most of the time when the player is holding a weapon the health bar goes negative.
My code:
public int healths = 10;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(healths == 0)
{
SceneManager.LoadScene("lose");
Cursor.visible = false;
Cursor.lockState = CursorLockMode.None;
}
}
private void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Enemy")
{
healths = healths - 1;
}
if(collision.gameObject.tag == "Lava")
{
healths = healths - 1;
}
}
}
The health bar code:
Text E;
public GameObject Player;
// Start is called before the first frame update
void Start()
{
E = GetComponent();
}
// Update is called once per frame
void Update()
{
E.text = "Health: " + Player.GetComponent<E>().healths;
}
}