- Home /
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'
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.
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'
Did you write this code? What exactly is the "move" variable intended for?
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/
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();
}
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.
Your answer
Follow this Question
Related Questions
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