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

public class AudioManager : MonoBehaviour {

    public GameObject audioPrefab;

    public void Play(AudioClip clip, Vector3 position, float pitch = 1.0f)
    {
        GameObject audioInst = GameObject.Instantiate(audioPrefab, position, Quaternion.identity);
        AudioSource source = audioInst.GetComponent<AudioSource>();
        source.clip = clip;
        source.pitch = pitch;
        source.Play();
        StartCoroutine(CheckDeleted(source));
    }

    IEnumerator CheckDeleted(AudioSource source)
    {
        while (source.isPlaying)
            yield return null;

        GameObject.Destroy(source.gameObject);
    }
}
