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

// Script that can spawn prefab from Event (made to use with a sound trigger)

public class SpawnWithSound : MonoBehaviour
{
    public bool spawnChildrenOfPrefab = false;
    public GameObject prefab;
    public float threshold = 0.5f;
    public bool randomRotation = true;

    public float Spawn {
        set {
            if(value > threshold) 
                spawn();
        }
    }

    int currentChild = 0;
    Transform[] children;
    void Start() {
        if(spawnChildrenOfPrefab){
            children = prefab.GetComponentsInChildren<Transform>();
            print(children.Length);
        }
    }

    void spawn() {
        Quaternion rot = (randomRotation) ? Random.rotation : transform.rotation;

        if(spawnChildrenOfPrefab){
            Transform newObj = Instantiate(children[currentChild], transform.position, rot);
            newObj.parent = transform;

            currentChild++;
            if(currentChild >= children.Length) currentChild = 0;
        } else {
            Instantiate(prefab, transform.position, rot);
        }
        
    }
}
