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

public class SpawnFans : MonoBehaviour {

    [SerializeField]
    public List<GameObject> fans;
    private GameObject fan;

    // Use this for initialization
    void Start () {
        spawn();
    }
	
	// Update is called once per frame
	void Update () {
        if (checkCountExist() <= 0)
        {
            spawn();
        }
    }

    int checkCountExist()
    {
        int count = fans.Count;
        foreach (GameObject currentFan in fans)
        {
            if (currentFan.GetComponent<FanController>().fanFollowing != null)
            {
                count--;
            }
        }
        return count;
    }

    void spawn()
    {
        fan = Resources.Load("Fan") as GameObject;
        for (var f=0; f<= Random.Range(3, 8); f++)
        {
            fans.Add(Instantiate(fan, new Vector3(Random.Range(-18.95f, 18.95f), 1.3f, Random.Range(-15.02f, 15.02f)), Quaternion.identity) as GameObject);
        }
    }
}
