- Home /
Jumping Problem
I am making a simple game and i need my player to jump. They way i want to do it i get the error: Operator '&&' cannot be applied to operands of type 'bool' and 'float'. Here is my script: public class Jump : MonoBehaviour { public Rigidbody rb; public float UpForce = 2000f; public float jumpCount;
void FixedUpdate()
{
if (Input.GetKey("w") && jumpCount = 0)
{
rb.AddForce(0, UpForce * Time.deltaTime, 0, ForceMode.Impulse);
jumpCount = 1f;
}
}
void OnCollisionEnter(Collision collisionInfo)
{
if (collisionInfo.collider.tag == "Floor")
{
jumpCount = 0f;
}
}
Sidenote: When dealing with a specific error like this, it's probably best to look up/ask questions around the error itself. When posting code, it's also generally better to only post the relevant code. Your script is small, but as it gets more complex, posting the entire code is much less likely to get you quality answers.
Basically, the problem here had nothing to do with Jumping, and all to do with using the && operator. In the future it's best to search/ask for how to fix your actual problem in the code, rather than how to fix what you intend to do with the code. A lot of people will likely have encountered the problem before, but for different reasons. I know for a fact that your error here is incredibly common, especially among coders who are either new or are co$$anonymous$$g from a C/C++ background.
Sometimes, just pasting the error message into google can get you headed in the right direction.
Answer by Zarenityx · Jul 03, 2019 at 03:15 PM
The problem is a really simple one:
You used = instead of ==.
Remember that = is assignment, == is comparison. That is, if you have two variables, A and B, both of type float
, then the expression
(A==B)
is actually a bool
, which you can use with the && operator or put into an if statement. But the expression
(A=B)
is one that will set A to the value of B, and also, because of how C# works, return the value of B (which is also now A) as a float
.
In your code, the problem line is:
if (Input.GetKey("w") && jumpCount = 0){
...
}
Now, if
expects a bool
, and, unlike C++ or other languages, will not accept any other type. Now, you are feeding it the expression
(Input.GetKey("w") && jumpCount = 0)
which uses the &&
operator. The &&
operator returns a bool
(just what you want for an if statement), and expects two bool
s, one on the left and one on the right. The left side of your AND operator is:
Input.GetKey("w")
which is a method that returns a bool
. Perfect. Now the right side of your AND operator is all that's left. The expression:
(jumpCount = 0)
This expression says "Set jumpCount to 0, then return the value of jumpCount afterwards.". Since jumpCount
is a float
, then the whole expression returns a float
. Since this is C#, which has fairly strict type enforcement, there's no way that a float
can just be treated as a bool
. So there lies your problem. You are trying to use && with a bool and a float, when it expects 2 bools.
What you probably meant to do is Check whether jumpCount was 0, returning TRUE if so and FALSE otherwise. That expression looks like this:
(jumpCount == 0) //Notice the == instead of =
Now, ==
is a comparison which returns a bool. So now you have a correct bool && bool
, instead of the incorrect bool && float
.
Your answer
![](https://koobas.hobune.stream/wayback/20220612204259im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
I am always getting this error when I jump, does anyone know how to fix it?? 0 Answers
if statement in a get function 1 Answer
Unity Activation Problem 2 Answers
Cubed's Unity Shader Error CS1502 0 Answers