Getting the Error "Cannot implicitly convert type 'float' to 'bool'. Seriously confused.

Hey so my goal with this code is to control an enemy’s movement as well as have them instantiate a projectile gameObject that flies ahead of them toward the player EVERY “throwRate” seconds.

Important info: My player is motionless, the shurikans are their own gameObject prefab, the enemy is suppose to get close to the player to a certain range and then stop while continuing to throw shurikans.

public float speed = 3f;
    public float throwRate = 10f;
    public GameObject weapon;
    public float throwSpeed = 5f;
   
    GameObject player;
    GameObject shurikan;
    private Vector3 target;
    float distance;
    float timer = 0f;
   
   
    void Start ()
    {
        player = GameObject.FindGameObjectWithTag("Player");
        target = player.transform.position;       
    }
   
    void Update ()
    {
        timer += Time.deltaTime;
        float distance = Vector3.Distance(transform.position, target);
        if (distance > 5)
        {
            transform.position = Vector3.MoveTowards(transform.position, target, speed * Time.deltaTime);
        }
       // this is the area of trouble.
        if (timer >= throwRate)
        {
            shurikan = Instantiate(Resources.Load ("Shurikan")) as GameObject;
            timer = 0f;
        }
    }

I’m getting the error about cannot convert float into a bool on line 28. Both timer and throwRate are floats and I’m trying to use a simple if statement. I’ve tried using a constant in place of throwRate but it still doesn’t work. I’m still new to Unity, thank you for any help you can offer.

I copy-pasted your code, and it works perfectly. Compiles, runs, instantiates the things it’s supposed to instantiate.

Is your error a compilation error, or does it happen when you run the game?

By the way, instead of using a timer, you should really look into using a coroutine. Those are usually a lot more readable, and once you get used to them, easier to use.

Oh, and it’s “Shuriken”, not “Shurikan”.

Weird, I’m getting a compilation error. It won’t let me run the game.

Coroutines… okay I’ll look into those. But I’m still stumped that it works for you :S

And yes, I should fix that typo. Thanks for pointing it out.

Double check what you have in your code compared to what is here. It sounds like you’re inadvertently handing the if statement a float instead of a bool. Maybe you have += instead of >=? Or even => which would be a lambda statement not a comparison. I’m confused too, the way it is typed here should be just ducky.

Thanks for your feedback. Actually after restarting the entire program it fixed itself. However I did copy and paste the code exactly from my project. So still weird :S

Your code probably got out of sync with the project, so the project never recompiled any code changes. Happens with MonoDevelop sometimes. There’s a solution, though.