Enemy Spawning Help (2D Platformer, C#)

Hi, I am going to have multiple spawn points with this script attached. And I want the enemies to spawn after a certain amount of time, but only when the player is in a certain range (“maxDistance”). And not spawn enemies at all when the player is not in that range. This part works, the problem is the when I start the game and after the (“spawnDelay”), It spawns enemies rapidly and doesn’t stop. It ignores the (“spawnTime”). Please help me with this.

Script (C#)

public class Spawner : MonoBehaviour {

    public float spawnTime;        // The amount of time between each spawn.
    public float spawnDelay;       // The amount of time before spawning starts.
    public GameObject[] enemies;        // Array of enemy prefabs.

    public int maxDistance;
    public Transform target;
    public Transform myTransform;

    void Awake()
    {
        myTransform = transform;
    }

    void Start()
    {
        GameObject stop = GameObject.FindGameObjectWithTag("Player");

        target = stop.transform;

        maxDistance = 7;
    }

    void FixedUpdate()
    {
        if (Vector3.Distance(target.position, myTransform.position) < maxDistance)
        {
            InvokeRepeating("Spawn", spawnDelay, spawnTime);
        }
    }

    void Spawn()
    {
        // Instantiate a random enemy.
        int ZombieIndex = Random.Range(0, enemies.Length);
        Instantiate(enemies[ZombieIndex], transform.position, transform.rotation);   
    }
}

Found the perfect solution!!!
Here is the script if anyone needs it:

public class Spawner : MonoBehaviour {

    public float spawnTime;        // The amount of time between each spawn.
    public float spawnDelay;       // The amount of time before spawning starts.
    public GameObject enemy;

    public int maxDistance;
    public Transform target;
    public Transform myTransform;

    void Awake()
    {
        myTransform = transform;

    }

    void Start()
    {
        GameObject stop = GameObject.FindGameObjectWithTag("Player");

        target = stop.transform;

        maxDistance = 5;

        StartCoroutine(SpawnTimeDelay());
    }

    IEnumerator SpawnTimeDelay()
    {
        while (true)
            {
                if (Vector3.Distance(target.position, myTransform.position) < maxDistance)
                {
                Instantiate(enemy, transform.position, Quaternion.identity);
                yield return new WaitForSeconds(spawnTime);
                }

                if (Vector3.Distance(target.position, myTransform.position) > maxDistance)
                {
                    yield return null;
                }                
            }
    }
}

I had to use a while loop with the WaitForSeconds statement. Thanks for no ones help :stuck_out_tongue: It took me for ever!

Instead of using InvokeRepeating just use: Spawn();