GetKey acting even when it shouldnt

So I am making an FPS game where the player has a fully automatic primary weapon, and a semi auto secondary. In order for the player to be able to shoot with the primary gun he needs to be holding the left mouse button and both “isActive” and “canShoot” need to be true. The code looks something like this:

void Update ()
{
if (isActive && canShoot && Input.GetKey("mouse 0")
{
Shoot();
}
}

The issue I am having, is when the player switches back to the primary weapon while holding mouse 0, it near instantly shoots the entire 30 round magazine, completely ignoring the fire rate I have set in Shoot(). isActive and canShoot are both false during this swapping gun period, so why does it still listen to the getkey, and seemingly shoot all the bullets from the time I spent holding mouse 0. I’ve tried writing this in all sorts of ways but haven’t been able to come up with a fix yet.

Did you copy and paste the code to this unity website? Because if so, you’ll need another ) after if (isActive && canShoot && Input.GetKey("mouse 0"), maybe that’s the issue?

Example:


Before: if (isActive && canShoot && Input.GetKey("mouse 0")

After: if (isActive && canShoot && Input.GetKey("mouse 0"))


  void Update ()
   {
         if (isActive && canShoot && Input.GetKey("mouse 0")
         {
               Shoot();
         }

Maybe the Input.GetKey(“mouse 0”) does’nt count because it thinks ) is the end of the if ()

I hope this helps!

I think your problem is(probably)

Shoot(); <-- this

// But use this instead of that

StartCoroutine(Shoot());