Rick,
Your method for stopping the coroutine is more complicated than it has to be. I implemented a different method when you challenged us and it does not have the firing bug, plus the code is much simpler. No need to save a handle to the coroutine and it stops itself automatically by simply exiting the method when no button is being pressed.
You can press either or both spacebar and mouse button 0, release either one while keeping the other pressed, and it keeps firing until both are no longer pressed.
[SerializeField]
float projectileFiringPeriod = 0.25f;
/***
* Fire() will handle firing the laser from the Player ship.
***/
private void Fire()
{
if (Input.GetButtonDown("Fire1"))
{
StartCoroutine(FireContinuously());
} // if
} // Fire()
/***
* FireContinuously() will fire the laser from the Player ship and keep
* firing as long as the space bar or mouse button 0 are pressed. Presumably
* it will also work with a joystick, but I don't have one to test with.
***/
private IEnumerator FireContinuously()
{
do
{ // Fire laser and check to see if we should keep firing
// Things to do before
Vector2 laserStart = new Vector2(transform.position.x,
transform.position.y + YPadding); // Start above ship
GameObject laser = Instantiate(laserPrefab,
laserStart,
Quaternion.identity) as GameObject;
laser.GetComponent<Rigidbody2D>().velocity = new Vector2(0, projectileSpeed);
Debug.Log("Laser fired, boss");
yield return new WaitForSeconds(projectileFiringPeriod);
// Things to do afterwards
Debug.Log("Checking to see if we should keep firing, boss");
} while (Input.GetButton("Fire1"));
Debug.Log("Firing has ceased, boss");
} // FireContinuously()