Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by animatoroftheunknown · Aug 25, 2014 at 05:06 AM · floatbool

Cannot implicitly convert type `bool' to `float'

 using UnityEngine;
 using System.Collections;
 
 public class CharController : MonoBehaviour 
 {
     public float maxSpeed = 10f;
     bool facingRight = true;
 
     bool grounded = false;
     public Transform groundCheck;
     float groundRadius = 0.2f;
     public LayerMask whatIsGround;
     public float jumpForce = 700f;
 
     void Start ()
     {}
 
     void FixedUpdate ()
     {
         grounded = Physics2D.OverlapCircle(groundCheck.position, groundRadius, whatIsGround);
 
         float move = Input.GetMouseButtonDown(0);
 
         rigidbody2D.velocity = new Vector2 (move * maxSpeed, rigidbody2D.velocity.y);
 
         if (move > 0 && !facingRight)
             Flip ();
         else if(move < 0 && facingRight)
             Flip();
 
     }
 
     void Flip()
     {
         facingRight = !facingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 
     void Update () 
     {
         if(grounded && Input.GetKeyDown(KeyCode.Space))
             rigidbody2D.AddForce(new Vector2(0, jumpForce));
     }        
 }


I keep getting this error: Assets/Scripts/CharController.cs(22,23): error CS0029: Cannot implicitly convert type bool' to float'

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Khada · Aug 25, 2014 at 05:08 AM

As robertbu said, The 'GetMouseButtonDown' method only returns a boolean value (true/false) and you're trying to store that in a float (number with decimal component) which doesn't really make sense.

Either change float to bool so that you're matching the return value with the variable type, or call a different method that returns a float.

Comment
Add comment · Show 3 · 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
avatar image animatoroftheunknown · Aug 25, 2014 at 04:15 PM 0
Share

Every time I change float to bool, I get this error: Assets/Scripts/CharController.cs(24,53): error CS0019: Operator *' cannot be applied to operands of type bool' and `float'

avatar image tanoshimi · Aug 25, 2014 at 07:37 PM 0
Share

Did you write this code? What exactly is the "move" variable intended for?

avatar image Khada · Sep 06, 2014 at 01:09 PM 0
Share

That's because on the next line of code you're treating the 'move' variable like a float. The underlying issue here is simply a fundamental misunderstanding of how variables work in C#.

I recommend beginning with some intro tutorials on the C# language before diving into program$$anonymous$$g a game using Unity + C#. Try this resource: http://www.learncs.org/

avatar image
0

Answer by Tomer-Barkan · Aug 25, 2014 at 05:12 AM

Input.GetMouseButtonDown(0) tells you whether the mouse button was first pressed in this frame or not. It does not indicate where the mouse is pressed, if you want to check that, you need to look at Input.mousePosition

If you want to flip every time the mouse is pressed, you need to use the following code:

 if (Input.GetMouseButtonDown(0)) {
     Flip();
 }


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
avatar image
0

Answer by Ekta-Mehta-D · Aug 25, 2014 at 05:12 AM

I think error is

 float move = Input.GetMouseButtonDown(0);


Have a look at this http://docs.unity3d.com/ScriptReference/Input.GetMouseButtonDown.html .

 Input.GetMouseButtonDown(0) returns bool.

Thanks.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

the run animation dont stops if i stop my charaster,hello, i cant stop the "run" animation, when speed == 0 1 Answer

Converting Float to Bool 2 Answers

Convert type `float' to `bool', Load 1 Answer

Timer - CS0029: Cannot implicitly convert type `float' to `bool' 1 Answer

Power System in Unity. 1 Answer


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