Error CS0029: Cannot implicitly convert type 'float' to 'bool'?

Hi, I’m very new to Unity, and I’m following tutorials my professor has been giving to my class, but I keep getting this error: “Assets\Scripts\EnemyProjectile.cs(19,12): error CS0029: Cannot implicitly convert type ‘float’ to ‘bool’” I’m trying to implement projectiles at the moment, but I cannot wrap my head around how to fix this error. The script is relatively short, and there are other scripts in my project too, but I doubt that they’re interfering with this one.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemyProjectile : MonoBehaviour
{
    public int damageAmount;
    public float speed;
    public float ifMoves;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (ifMoves)
        {
            transform.Translate(Vector2.down * speed * Time.deltaTime);
        }
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if(collision.tag == "Player")
        {
            collision.GetComponent<SpaceshipControls>().TakeDamage(damageAmount);
            Destroy(gameObject);
        }
    }
}

Well, it’s pretty clear you haven’t been following them very closely :slightly_smiling_face:.

I won’t give you the answer —I’m against providing solutions to class projects (as are many others on these forums)— because these tutorials usually contain all the information you need to solve the problem. But I can give you some hints:

  • Look closely at the error message. It tells you the exact line and column where the error occurs , in this case, line 19, column 12. What’s there?
  • The error also explains what’s wrong: it expected a variable of type bool, but instead it found a variable of type float and couldn’t convert it.

So, go to the indicated location, find the variable, and then correct its type.

I noticed the issue a little after I posted it, lol! I typed public float rather than public bool. Thanks for your insight :>

Additionally, ifMoves is a confusing name for a boolean variable, as the expression if (ifMoves) doesn’t read well. A more appropriate name would be isMoving.