using UnityEngine;
using System.Collections.Generic;

public class SpawnController : MonoBehaviour
{

    public List<GameObject> obstacles;
    public List<GameObject> activeObstacles;
    private int ticks = 0;
    public int nextObjectSpawn = 20;
    public float yMin;
    public float yMax;

    public int nextMin;
    public int nextMax;

    // Use this for initialization
    void Start()
    {
        activeObstacles = new List<GameObject>();
    }

    // Update is called once per frame
    void Update()
    {
        if (((++ticks) % nextObjectSpawn) == 0)
        {
            CreateSpawnObject();
            nextObjectSpawn = Random.Range(nextMin, nextMax);
        }
    }

    void CreateSpawnObject()
    {
        var randomIndex = Random.Range(0, obstacles.Count);
        var obstacle = Instantiate(obstacles[randomIndex]);
        obstacle.transform.parent = gameObject.transform;
        var xPos = 30f;
        var yPos = Random.Range(yMin, yMax);
        obstacle.transform.position = new Vector3(xPos, yPos, 0);
        activeObstacles.Add(obstacle);
    }
}
