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

public class SpawnCube : MonoBehaviour
{
    public GameObject prefab;
    public int numberToSpawn = 10;
    public float spawnInterval = 1f;
    public float spawnRange = 1f;

    float lastTime = 0;
    void Start()
    {
        //for(int i=0; i < numberToSpawn; i++)
        //{
        //    Spawn();
        //}

        //InvokeRepeating("Spawn", 0, spawnInterval);

        StartCoroutine(SpawnAtInterval());
    }

    void Update()
    {
        //if(Time.time > lastTime + spawnInterval)
        //{
        //    Spawn();
        //    lastTime = Time.time;
        //}
    }

    void Spawn()
    {
        Vector3 pos = transform.position + Random.insideUnitSphere * spawnRange;
        Quaternion rot = transform.rotation;
        Instantiate(prefab, pos, rot);
    }

    IEnumerator SpawnAtInterval()
    {
        while (true)
        {
            Spawn();
            yield return new WaitForSeconds(spawnInterval);
        }
    }
}
