Search Unity

Bug Value is always 0

Discussion in 'Scripting' started by Joggla, Dec 2, 2022.

  1. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    This script used to work... and now suddenly it doesent work anymore...


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HPregen : Ability
    6. {
    7.     public float healpercent;
    8.     public float tickrate;
    9.    
    10.     public override void AbilityEffect(Unit unit)
    11.     {
    12.         if(timer >= tickrate)
    13.         {
    14.             timer = 0;
    15.             int healamount = (int)((unit.baseHP / 100) * healpercent);
    16.             Debug.Log(healamount);
    17.             if(healamount <= 0)
    18.             {
    19.                 healamount = 1;
    20.             }
    21.             unit.currentHP += healamount;
    22.             if(unit.currentHP > unit.baseHP)
    23.             {
    24.                 unit.currentHP = unit.baseHP;
    25.             }
    26.             unit.UpdateHealthbar();
    27.         }
    28.     }
    29. }
    The Debug.Log(healamount); is always 0
     
  2. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    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.
     
    Joggla and Kurt-Dekker like this.
  3. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    I added 2 debugs ->
    the math should work, wtf is happening
    how could i change this calculation to make it work

    Code (CSharp):
    1. Debug.Log(unit.baseHP);
    2. Debug.Log(healpercent);
    upload_2022-12-2_23-33-28.png

    (47 / 100) * 10 should be more than 0
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    (47/100)
    is always going to be 0 if those are integers

    That's what @halley 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
     
    Bunny83 and Joggla like this.
  5. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    I just did a small test:
    upload_2022-12-2_23-40-17.png
    yeah mb, this is also 0
     
  6. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    So try some variations.
    Does moving healpercent to the front help?
    Do removing some parentheses help?
    Does multiplying by healpercent first help?
     
    Joggla likes this.
  7. Joggla

    Joggla

    Joined:
    Dec 2, 2019
    Posts:
    88
    upload_2022-12-2_23-43-44.png
    yep, ty alot guys, this now works and the result is 4 instead of 0
     
  8. Max-om

    Max-om

    Joined:
    Aug 9, 2017
    Posts:
    499
    Defining 100 as an float 100f would also had solved above problem
     
    Joggla likes this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Yep, same reason. Integer divide.