A small issue with co routines for a gun fire rate

Ok so i have been working a researching on a way to get a fire rate for the gun im making in a game so the time between each shoot is fired. i had to do this otherwise it fires evry frame which just doesn’t work. i have written what i think should work for what i need but keep getting this error

error CS1624: The body of EnemyFirescript.FireEnemyGun1()' cannot be an iterator block because void’ is not an iterator interface type

i have written this so far
using UnityEngine;
using System.Collections;

public class EnemyFirescript : MonoBehaviour {

	public GameObject Gen1AlienLaser;				// Prefab of the laser.
	public float speed = 20f;				// The time between when the lasers will be fired.
	
	public float firerate =4.0f;
	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
// unused called from separate script. 
		
		
	}

public void FireEnemyGun1 ()
	{

	
		yield return WaitForSeconds(firerate);
		GameObject bulletInstance = (GameObject)Instantiate(Gen1AlienLaser, transform.position,  transform.rotation);
		bulletInstance.rigidbody2D.velocity = (transform.right*speed);
		}
		
		


void Firerate ()

	{
		StartCoroutine( Firerate() );
	}
}

im not sure where i have gone wrong and the research i have done hasn’t come up with any results any help would be appreciated thanks.

First a coroutine needs to return IEnumerator and not void, this is what the error says.Also, you are calling the method itself, I guess you meant to call FireEnemyGun1 in the StartCoroutine.

Second, even though you would fix the issue, I doubt this is about to work as expected. As the rest of the code is not showed I cannot really say but I would suspect that you want Firerate to prevent a new shot before 4 seconds. What is about to happen is that if you press 20 times in a row (frenetically almost retard), you will have nothing for 4s and then you will have all of them coming at once or almost (depending on how frenetic/retard you can get).

What you need is prevent the call of the coroutine as well:

private bool waiting = false;
private IEnumerator FireEnemyGun1 ()
{
   waiting = true;
   yield return WaitForSeconds(firerate);
   GameObject bulletInstance = (GameObject)Instantiate(Gen1AlienLaser, transform.position,  transform.rotation);
   bulletInstance.rigidbody2D.velocity = (transform.right*speed);
   waiting = false;
}

void Firerate ()
{
   if(!waiting) 
       StartCoroutine( FireEnemyGun1() );
}

}

The problem with the error is that a function must return an IEnumerator so line 20 should be:

public IEnumerator FireEnemyGun1 ()

And unless you plan on calling it from another class, it does not need to be ‘public’. But even if you fix this problem, the pathway you are heading down does not get you what you want. That is, it will cause a pause of 4 seconds (the default value of ‘firerate’) before the bullet comes out of the gun, but it will not stop ‘FireEnemyGun1’ from being called multiple times. So if the user manages to pull the trigger 4 times in a second, each bullet will wait 2 seconds before coming out of the gun, but they will all be about 1/4 of a second apart. Here is more information on coroutines:

http://unitygems.com/coroutines/

There are lots of approaches to get what you want. The simplest is a timestamp. That is each time you fire the gun, you record the future time when the gun will be available. If Time.time is less than that ‘timestamp’, attempts to fire the gun are ignored. Here is question as reference:

http://answers.unity3d.com/questions/589887/machine-gun-script-.html