Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by osinator14 · Jul 03, 2019 at 02:57 PM · error messagejumping object

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;
     }
 }





Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zarenityx · Jul 03, 2019 at 03:20 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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 bools, 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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

I am always getting this error when I jump, does anyone know how to fix it?? 0 Answers

ArgumentException: GetComponent requires that the requested component 'List`1' derives from MonoBehaviour or Component or is an interface. 1 Answer

if statement in a get function 1 Answer

Unity Activation Problem 2 Answers

Cubed's Unity Shader Error CS1502 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges