Free script u can use for random background music. "Fixed Problem"

God please end my suffering I’ve been at this for an hour and the same bug keeps popping up and all I’m trying todo is make a simple bit of code I can send to my friend so he can understand AudioClip randomization smh

public class MusicPlayer : MonoBehaviour
    //Add Audio Source to MainCamera
    //If it isnt called "Main Camera" then make it public and drag it in
    //also highlight the bit in the Start funtion
    private AudioSource CamAudioSource;

    //Put the music in here so it works in the background
    public AudioClip[] audioSources;

    //When this is false Music/Sfx will play
    public bool isPlayingMusic;

    //Delete this later
    public AudioClip boing;

    // Start is called before the first frame update
    void Start()
        CamAudioSource = GameObject.Find("Main Camera").GetComponent<AudioSource>();

    private void Update()
        if (!isPlayingMusic)

            //Get a random Music/Sfx from the Array
            CamAudioSource.clip = audioSources[Random.Range(0, audioSources.Length)];

            //Play random Clip that has been added into the Array

            //Fire the IEnumerator/Coroutine that has been put in

    IEnumerator RandomTime(float TimeTillNextSFX)
        //Stops the if statment from Starting a new Couroutine
        isPlayingMusic = true;

        //Adding Time between how long the clip is and then when it plays again
        float WaitTime = TimeTillNextSFX + Random.Range(20f, 120f);

        //Shows u when the Music/Sfx are gonna fire again
        Debug.Log("Will Fire Again at: " + WaitTime + " seconds.");

        //Waiting now for the song to finish and then the extra time between Music/Sfx
        yield return new WaitForSeconds(WaitTime);

        //Make the if statement fire again
        isPlayingMusic = false;

Turns out u shouldn’t have scripts on objects that u wont be using the arrays with,

nothing is wrong with the code just have to copy and past it in and it works right away