Question: How do I create a time interval for pizza spawning? During the game time for every 15s start spawning pizza for 5 seconds and stop, then start spawning pizza again after 15s

```
   IEnumerator SpawnPizzaCoroutine()
    {
        while (true)
        {
            int spawnPointX = Random.Range(-18, -5);
            int spawnPointY = Random.Range(5, 0);
            Vector3 spawnPosition = new Vector3(spawnPointX, spawnPointY, 0);
            Instantiate(Pizza, spawnPosition, Quaternion.identity);
            yield return new WaitForSeconds(3f);
        }
    }
void Start()
    {
        StartCoroutine(SpawnPizzaCoroutine());
    }

```

Answer: 
You just need to keep track of the elapsed time between pizzas in a float type variable

```
private bool spawningPizza;
private float nextSpawnTime;

private void Start()
{
    StartCoroutine(SpawnPizzaCoroutine());
}

private IEnumerator SpawnPizzaCoroutine()
{
    while (true)
    {
        nextSpawnTime += Time.deltaTime;

        if (nextSpawnTime >= 15)
        {
            spawningPizza = true;

            int spawnPointX = Random.Range(-18, -5);
            int spawnPointY = Random.Range(5, 0);
            Vector3 spawnPosition = new Vector3(spawnPointX, spawnPointY, 0);
            Instantiate(Pizza, spawnPosition, Quaternion.identity);

            yield return new WaitForSeconds(5f);

            nextSpawnTime = 0;
            spawningPizza = false;
        }
    }
}

```
I would use [`InvokeRepeating`](https://docs.unity3d.com/ScriptReference/MonoBehaviour.InvokeRepeating.html) for such a scenario. That calls a method for every x seconds.

```
public class PizzaMaker: MonoBehaviour
{
    public GameObject pizza;

    void Start()
    {
        InvokeRepeating("makePizza", 15f, 15f);
    }

    void makePizza()
    {
        int spawnPointX = Random.Range(-18, -5);
        int spawnPointY = Random.Range(5, 0);
        Vector3 spawnPosition = new Vector3(spawnPointX, spawnPointY, 0);
        Instantiate(pizza, spawnPosition, Quaternion.identity);
    }
}

```