StartCouroutine doesn't work with If statements?

So I have it set so that the player is static located at one part of the map, the enemy is moving towards the player using a nav mesh, so I calculate the distance before it’s near the player then starts shooting at the player. Problem here is couroutine initiates right at the beginning and prevents me from using a if statement to control it. Thanks for your time reading this!

using UnityEngine;
using System.Collections;

public class NavigationScript : MonoBehaviour 
{

	public float distance;
	public GameObject player;
	private NavMeshAgent agent;
	public Rigidbody bullet;
	public float fireSpeed;
	public Transform posFire;
	public float delay = 2;


	// Use this for initialization
	void Start () 
	{
		player = GameObject.FindGameObjectWithTag("Player");
		agent = GetComponent<NavMeshAgent>();
		StartCoroutine (Shooting());
	}

	
	// Update is called once per frame
	void Update () 
	{
		GetComponent<NavMeshAgent>().destination = player.transform.position;
		distance = Vector3.Distance(player.transform.position, agent.transform.position);
	}

	IEnumerator Shooting()
	{
		if(distance < 160.0f)
		{
			for(int i = 0; i < 100; i++)
			{

				Rigidbody SpawnBullet = Instantiate(bullet, posFire.position, transform.rotation) as Rigidbody;
				SpawnBullet.velocity = transform.TransformDirection(new Vector3(0,0,fireSpeed));
				yield return new WaitForSeconds (delay);
			}
		}
	}
}

The problem here is that the coroutine will immediately complete and exit if the first time its called the distance is greater or equal to 160. since the if statement will fail, the code will run to the end of the routine and it will finish.

Coroutines can be used like Update but unlike update, they can finish. If you want to continue running the coroutine all the time like Update. Then just wrap the entire if statement in a while(true) loop. with a "yield return null; " at the end of it.

EDIT: Re-looking at your code, you may actually want to move your seconds delay to replace the return null. That will cause the coroutine to run with the slower frequency. if that is what you’re going for.

IEnumerator Shooting()
{
     while(true)
     {
          if(distance < 160.0f)
          {
               // Your other stuff here.
          }
          yield return null;
     }
}