using System.Collections.Generic;
using UnityEngine;

public class AudioSourcePool : MonoBehaviour
{
    [SerializeField] private AudioSource audioSourcePrefab;
    private List<AudioSource> audioSources = new();

    public static AudioSourcePool instance;
    private void Awake()
    {
        if (instance == null) instance = this;
        else Destroy(gameObject);
        DontDestroyOnLoad(gameObject);
    }

    public AudioSource GetSource()
    {
        foreach (AudioSource source in audioSources)
            if (!source.isPlaying) return source;
        AudioSource newAudio = Instantiate(audioSourcePrefab,transform);
        audioSources.Add(newAudio);
        return newAudio;
    }
}
