Xczmike2428
Xczmike2428

Reputation: 1

if statement cant apply the operator '<=' at the operators of type 'bool' and 'float'

bool isMouseClicked;
bool Test;
float Number1;
float Number2;
float Number3;

isMouseClicked = true;
Number1 = -1;
Number2 = 0;
Number3 = 1; 

void Update()
{
   if (isMouseClicked && Number1 <= Number2 <= Number3)
   {
      Test = true;
   }
}

When I run this program it tells me Operator '<=' cannot be applied to operands of type 'bool' and 'float'.

Upvotes: 0

Views: 48

Answers (1)

David
David

Reputation: 218847

This:

Number1 <= Number2 <= Number3

Means this:

(Number1 <= Number2) <= Number3

And a number can not be greater than or less than a boolean value.

Basically, you're thinking about this in terms of intuitive human languages. Don't. Think about this in terms of logical conditions. What are the conditions you want to express?

You want to express this:

Number1 <= Number2

And this:

Number2 <= Number3

The language used says it all... "this and this". Combine the operations exactly like that:

Number1 <= Number2 && Number2 <= Number3

So overall each logical operation is its own distinct condition being checked:

if (
  isMouseClicked
  && Number1 <= Number2
  && Number2 <= Number3
)

Upvotes: 5

Related Questions