# Value is always 0

This script used to work… and now suddenly it doesent work anymore…

``````using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HPregen : Ability
{
public float healpercent;
public float tickrate;

public override void AbilityEffect(Unit unit)
{
if(timer >= tickrate)
{
timer = 0;
int healamount = (int)((unit.baseHP / 100) * healpercent);
Debug.Log(healamount);
if(healamount <= 0)
{
healamount = 1;
}
unit.currentHP += healamount;
if(unit.currentHP > unit.baseHP)
{
unit.currentHP = unit.baseHP;
}
unit.UpdateHealthbar();
}
}
}
``````

The Debug.Log(healamount); is always 0

Before someone else pops in with a whole lot of tips on debugging, look at line 15 and decide “what would make that result in 0?” In general, when doing math on computers, especially with integers but even with floats, you want to do multiplies before divides.

2 Likes

the math should work, wtf is happening
how could i change this calculation to make it work

``````Debug.Log(unit.baseHP);
Debug.Log(healpercent);
``````

(47 / 100) * 10 should be more than 0

`(47/100)` is always going to be 0 if those are integers

That’s what @halley1 is pointing out.

So for percentages, multiply first, THEN divide, then cast back to int

EDIT: in related news, `(100/47)` is always going to be 2, if those are integers

2 Likes

I just did a small test:

yeah mb, this is also 0

So try some variations.
Does moving healpercent to the front help?
Do removing some parentheses help?
Does multiplying by healpercent first help?

1 Like

yep, ty alot guys, this now works and the result is 4 instead of 0

Defining 100 as an float 100f would also had solved above problem

1 Like

Yep, same reason. Integer divide.