Unity thinks float is bool or int

Hi
I’ve made a script with this if statement

if (1 > currentColour > 0)

when I saved this and tried to run game in Unity I got error
Operator ‘>’ cannot be applied to operands of type ‘bool’ and ‘int’
currentColour is defined as float

public float currentColour;

What’s the problem?

first part returns true or false, so its a bool in that sense:

(1 > currentColour)

what are you trying to do with that if?

This is not proper syntax. should be:

if ((1 > currentColour) && (currentColour > 0))
2 Likes

@kdgalla thanks! That works!

if () statements are just blocks that evaluate conditions that are required to evaluate to true or false. If all of the conditions are true, then it enters the if block. When any of them are false, it doesn’t. (unless you’re doing niche, weird stuff - but lets not get confused)

            if (currentColour > 0 && currentColour < 1)

The && operator means AND… So its saying if color is greater than zero AND color is less than 1. Basically only if it’s between 0 and 1 does it enter the statement.

You can also do stuff like this that sometimes makes more complex conditions easier to read.

bool isValidated             = someClass.ColorsReady;
bool colorRangeIsValid       = colorInputs.currentColor > 0 && colorInputs.currentColor < 1;
bool customColorsEnabled     = colorInputs.useCustomColors;

if (isValidated && colorRangeIsValid && customColorsEnabled)
{
    // do work
}
1 Like

Sounds like your problem is solved, but just for the sake of completeness, let’s discuss how the computer was interpreting your original code, and why it reacted as it did.

The computer always interprets things one step at a time. For instance, if you wrote “a + b + c”, the computer doesn’t see “take the sum of these three numbers”, it sees “add a + b, and then add c to the result”. It’s adding in implied parentheses so that the statement becomes “(a + b) + c”.

You wrote:
if (1 > currentColour > 0)

You wanted that to be read as “if currentColor is between 1 and 0”. But the computer interprets it one step at a time. It adds some implied parentheses, like this:
if ((1 > currentColour) > 0)

So first it evaluates “1 > currentColour”, which results in an answer of either TRUE or FALSE. Then, it moves on to the second step, and tries to evaluate either “true > 0” or “false > 0”.

And at that point it gives up and tells you there’s an error, because those statements don’t make sense.

As an aside, this is an example of why strong typing in computer languages is a really good idea. C# is strongly-typed, which means it knows that a bool is a bool and it won’t let you try and use it as if it were a number. In a weakly-typed language, the computer would probably have decided that “true > 0” did make sense, and it would have happily computed a result and continued on with the program–even though that result would have had absolutely no relation to what you thought the computer was doing.

Then your program would give an error on some later line, totally unrelated to your actual mistake (or even make it to the end of the program and simply give a totally-wrong answer), and you might have been scratching your head for hours or days trying to figure out what was going wrong.

2 Likes