using System.Collections; using System.Collections.Generic; using UnityEngine; public class SoundManager : MonoBehaviour { // Getting an array of songs I used for ambience [SerializeField] AudioClip[] ambienceFX; // Components AudioSource audioSource; // Method runs on the first frame void Start() { // Getting components audioSource = GetComponent(); // Starting play random track method PlayRandomTrack(); } // Method that plays a random track void PlayRandomTrack() { // Uses random library to pick a song from array int randomIndex = Random.Range(0, ambienceFX.Length); audioSource.clip = ambienceFX[randomIndex]; audioSource.Play(); // After song is finished rerun the method Invoke("PlayRandomTrack", audioSource.clip.length); } }