Using division/fraction for a result not working

Seems silly to me, but I have something like this:

float answer;

answer = 1 / 3;

answer comes out 0 in the inspector window.

If I instead use

answer = 0.3333333333333333f

it comes out fine. However, I prefer to use the fraction and have the game do the calculation. How do I make it do this?

Seems if I do

answer = 1/3f;

it works.

1 Like

That’s right. C#, like most C-derived languages, does integer division when the two operands are integers. So 1 divided by 3, in integer math, is 0.

If either operand is a float (such as 3f or 3.0f), then it will use float math instead.

3 Likes