Trying to spawn animals with an interval of 2 seconds using Coroutines and instead it spawn every millisecond

With the use of Coroutine, I want the animals to spawn every 1 second but instead, What happens is they spawn every millisecond. This is how it looks:


As you can see from the photo above, they fill the whole scene. It’s chaotic, I want it to look like every second or 2 like this:

Here is my code:

using System.Collections;
using System.Collections.Generic;
using UnityEngine.Pool;
using UnityEngine;

public class AnimalSpawnerScript : MonoBehaviour
{
    [SerializeField]
    private AnimalScript[] _animalPrefabs;
    private ObjectPool<AnimalScript> _animalObjectPooling;
    private int _minRandomXRange = -20;
    private int _maxRandomXRange = 20;
    Vector3 _randomAnimalSpawnSelector;
    private int _timerCountdown = 1; //to controls the spawn interval

    void Start()
    {
        //setup for object pooling
        _animalObjectPooling = new ObjectPool<AnimalScript>(CreateAnimal, OnTakeAnimalFromPool, OnReturnAnimalToPool, OnDestroyAnimal, true, 60, 120);
    }

    // Update is called once per frame
    void Update()
    {
        SpawnAnimal();
        StartCoroutine(SpawnAnimalintervals());
    }

    //spawns animal if you press F
    private void SpawnAnimal()
    {
        if (Input.GetKeyDown(KeyCode.F))
        {
            _animalObjectPooling.Get();
        }
    }

    //spawn animals at intervals that fail and will spawn every millisecond
    IEnumerator SpawnAnimalintervals()
    {
        yield return new WaitForSeconds(_timerCountdown); //wait for _timerCountdown to countdown
        _animalObjectPooling.Get(); //spawn animal then go back to the top and wait for _timerCountdown to countdown again
    }

    //instantiates game object if pool is empty
    private AnimalScript CreateAnimal()
    {
        var _randomAnimalSelector = Random.Range(0, _animalPrefabs.Length);
        var _randomAnimalPrefab = _animalPrefabs[_randomAnimalSelector];

        // I use the rotation of the prefab, but you can use the rotation of any existing object
        var animal = Instantiate(_randomAnimalPrefab, _randomAnimalSpawnSelector, _randomAnimalPrefab.transform.rotation);

        // We make sure the pool reference is assigned to the instance created
        animal.SetPool(_animalObjectPooling);

        return animal;

    }

    //takes from the pool
    private void OnTakeAnimalFromPool(AnimalScript animal)
    {
        _randomAnimalSpawnSelector = new Vector3(Random.Range(_minRandomXRange, _maxRandomXRange), 0, 30);

        // Set transform and rotation to the animal coming from the pool
        animal.transform.position = _randomAnimalSpawnSelector;

        // Activate the animal coming form the pool
        animal.gameObject.SetActive(true);
    }

    //release from the pool
    private void OnReturnAnimalToPool(AnimalScript animal)
    {
        animal.gameObject.SetActive(false);
    }

    //destroys gameobject if pool reaches maximum capacity
    private void OnDestroyAnimal(AnimalScript animal)
    {
        Destroy(animal.gameObject);
    }

}

I have 2 ways of spawning the animals, one is by pressing the F and the other is using the coroutine and It doesn’t work how I want it to. How do I fix these?

This is happening because you are starting the coroutine every frame in Update()
Try putting the line StartCoroutine(SpawnAnimalintervals()); in Start() instead.