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

public class ObjectSpawner : MonoBehaviour {

	public GameObject[] spawnList;
	public float[] probabilityList;
	public float spawnTime;
	public float randomDelay;

	void Start () {
		StartCoroutine(SpawnItem());
	}

	IEnumerator SpawnItem() {
		// Determine which one to spawn from the list
		float rand = Random.Range(0f,1.0f);
		float temp = 0;
		int n = 0;
		for(int i=0;i<spawnList.Length;i++) {
			temp += probabilityList[i];
			if(temp >= rand) {
				n = i;
				break;
			}
		}
		Instantiate(spawnList[n],gameObject.transform.position,Quaternion.identity);

		// Random wait for new object to spawn
		yield return new WaitForSeconds(Random.Range(spawnTime, spawnTime + randomDelay));
		StartCoroutine(SpawnItem());
	}
}
