Fire rate for gun script, c# (not u/s)

Hello. I just started designing and testing the shooting SYSTEM first. I want my shooting to be controlled by a firerate(float) and use yield WaitForSeconds and bool ‘canfire = false/true’ Like this:

1st line: canfire = true; ///

2nd line: Yield WaitForSeconds blah blah ///

3rd line: canfire = true; /////////////////

So first canfire is disabled, wait seconds (firerate), then be able to fire again. Unfortunatly, C# doesn’t work as simple as JavaScript. I used IEnumerator and StartCoroutine but it didn’t work well. How? I had bullets–; before the yield so it made the bullet count drop to negative and all the way down and so on in a matter of seconds. Is there a way to not use coroutine? If not, can I make a different void and call it firerate and use coroutine on that? Or, how can I fix my script? Script:`using UnityEngine;
using System.Collections;

public class GunScript : MonoBehaviour
public int loaded;
public bool canfire;
public bool reloading;

void Update () 
	if(loaded > 0 && !reloading)
		canfire = true;
	if(loaded == 0)
		canfire = false;
		if(!reloading && canfire == true)

IEnumerator fire ()
	canfire = false;
	yield return new WaitForSeconds(firerate);
	canfire = true;
	Debug.Log("Ready to fire!");


Don’t worry about the bracket errors, the insert code here is glitchy… Also, sorry about the bad sorting at the beginning of the script, the insert code here again did that while I was editing it… UNITY PLEASE FIX!!!

It is unbelievably simple to do this using




Just read the doco.

beginners should never use yield, or touch the run loop at all, ever, for any reason.

once you master the trivial functions Invoke and CancelInvoke - which Unity put in the engine for a reason - you can learn the advanced stuff mentioned by experts on this question.