Search Unity

formula for chance returns "0"

Discussion in 'Scripting' started by Dawnreaver, Apr 7, 2013.

  1. Dawnreaver

    Dawnreaver

    Joined:
    Oct 13, 2010
    Posts:
    131
    Hey Folks,

    I ran into a strange problem: When I try to calculate a chance, it returns zero even though none of the elements of the formula are zero. (I debugged all elements of the formula)

    Code (csharp):
    1.  
    2. static public int  numberOfOnesUsedOnDice = 1;
    3. static public int usedDice = 1;
    4. static public float chanceToRoleOne =0.0f;
    5.  
    6. static public void setChances()
    7.     {
    8.         chanceCoins = (numberOfOnesUsedOnDice / (6 * usedDice)) * 100.0f;
    9. }
    10.  
    11.  
    I call the function from a different script when I press a button. Given the debugs all values are properly set/ called during the whole process. But as already mentioned all variables used in the formula are != 0.

    Any idea on why the formula would return 0?

    Kind Regards

    Dawnreaver
     
  2. ardo314

    ardo314

    Joined:
    Jul 7, 2012
    Posts:
    345
    You divide int by an int so the result will be int, so if theres something behind the dot it will be cut off so 0.6 will result in 0.
    Use 6f or cast usedDice or numberOfOnesUsedOnDice to float.
     
  3. EliteMossy

    EliteMossy

    Joined:
    Dec 2, 2012
    Posts:
    513
    Why not use Random.Range and test the value if its lower than a value or higher than a value etc.
     
  4. Dawnreaver

    Dawnreaver

    Joined:
    Oct 13, 2010
    Posts:
    131
    Thanks I'll give it a go :)