not sure what I’m missing so got character taking damage and the print statements come up just fine when hit 80-60-40-20-0 (dead) etc but my health bar when I take the first hit it just instantly drops to 0.
heres the code I’m using any help would be amazing.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DamageScript : MonoBehaviour {
public int currentHealth = 100;
public int maxHealth = 100;
int damage = 20;
public Image currentHealthBar;
public Text ratioText;
void Start()
{
maxHealth = 100;
currentHealth = maxHealth;
UpdateHealthbar ();
}
public void UpdateHealthbar()
{
int ratio = currentHealth / maxHealth;
currentHealthBar.rectTransform.localScale = new Vector3 (ratio, 1, 1);
ratioText.text = (ratio * 100).ToString ("0") + '%';
}
void OnCollisionEnter (Collision col)
{
if (col.gameObject.tag == "Bullet") {
currentHealth -= damage;
print ("hurt" + currentHealth);
if (currentHealth <= 0)
Destroy (gameObject);
{
UpdateHealthbar ();
} if (col.gameObject.tag == "Enemy") {
currentHealth -= damage;
UpdateHealthbar ();
print ("hurt" + currentHealth);
if (currentHealth <= 0)
Destroy (gameObject);
}
}
}
}