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

// Simple script to spawn a custom prefab object
public class SpawnGameObject : MonoBehaviour
{
    public GameObject prefab;
    [Range(0.1f,5f)]
    public float ratePerSecond = 1f;
    public bool randomRotation = false;
    public bool spawnAsChild = false;

    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(Spawn());
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    IEnumerator Spawn(){
        while(true){
            Quaternion rot = (randomRotation) ? Random.rotation : transform.rotation;

            GameObject newObject = Instantiate(prefab, transform.position, rot);
            
            if(spawnAsChild)
                newObject.transform.parent = transform;
            
            yield return new WaitForSeconds(1/ratePerSecond);
        }
        
    }
}
