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

[RequireComponent(typeof(AudioSource))]
public class RandomMusicSelectScript : MonoBehaviour
{
    [SerializeField] List<AudioClip> _MusicClipList;

    private AudioSource _Audio;

    // Start is called before the first frame update
    void Start()
    {
        _Audio = GetComponent<AudioSource>();
        _Audio.clip = GetRandomObject<AudioClip>(_MusicClipList);
        _Audio.Play();
    }

    T GetRandomObject<T>(List<T> list)
    {
        // проверка на пустоту списка
        if (list == null || list.Count == 0)
        {
            return default;
        }

        // Генерация случайного индекса
        int randomIndex = UnityEngine.Random.Range(0, list.Count);
        return list[randomIndex];
    }
}
