Hey there! I’m trying to make a simple thing: damage. I’m using a Hearts System for the Player Health like in Zelda games, but it keeps telling me that “Assets/Scripts/Working Scripts/HeartSystem.cs(63,17): error CS0266: Cannot implicitly convert type “float” to “int”. An explicit conversion exists (are you missing a cast?)”.
HeartSystem Script:
private int maxHeartAmount = 10;
public int startHearts = 3;
public int curHealth;
private int maxHealth;
private int healthPerHeart = 2;
public Image[] healthImages;
public Sprite[] healthSprites;
void Start () {
curHealth = startHearts * healthPerHeart;
maxHealth = maxHeartAmount * healthPerHeart;
checkHealthAmount ();
}
void checkHealthAmount () {
for (int i = 0; i < maxHeartAmount; i++) {
if (startHearts <= i) {
healthImages *.enabled = false;*
-
} else {*
_ healthImages .enabled = true;_
* }*
* }*
* UpdateHearts ();*
* }*
* void UpdateHearts(){*
* bool empty = false;*
* int i = 0;*
* foreach (Image image in healthImages){*
* if (empty) {*
* image.sprite = healthSprites [0];*
* } else {*
* i++;*
_ if (curHealth >= i * healthPerHeart) {
* image.sprite = healthSprites [healthSprites.Length - 1];*
* } else {*
int currentHeartHealth = (int)(healthPerHeart - (healthPerHeart * i - curHealth));
* int healthPerImage = healthPerHeart / (healthSprites.Length - 1);*
* int imageIndex = currentHeartHealth / healthPerImage;*
* image.sprite = healthSprites [imageIndex];*
* empty = true;*
* }*
* }*
* }*
* }*
* public void TakeDamage(float amount){*
* curHealth -= amount;*
curHealth = Mathf.Clamp (curHealth, 0, startHearts * healthPerHeart);_
* checkHealthAmount ();*
* }*
* public void AddHeartContainer(){*
* startHearts++;*
* startHearts = Mathf.Clamp (startHearts, 0, maxHeartAmount);*
* checkHealthAmount ();*
* }*
Damage script:
* float damage = -5f;*
* void OnTriggerEnter(Collider other){{*
* other.gameObject.GetComponent ().TakeDamage (damage);*
* }*
}
Thx for the help ![]()