Assign clip to audio source

Hello,

I have a prefab with an audiosource attached. The AudioClip variable box is empty, I want to assign it using a script.

Here's what I'm using to do that:

var clips : AudioClip[];
var rand = false;

function Start()
{
        audio.clip = clips[Random.Range(0,clips.Length)];
        RandSong();
}
function Update()
{
    if(rand)
    {
        RandSong();
        rand = false;
    }
}
function RandSong()
{
    audio.Play();
    yield WaitForSeconds (audio.clip.length);
    audio.clip = clips[Random.Range(0,clips.Length)];
    rand = true;
}

Then, on the same prefab, I fill in a few slots of the clips[] array with music. However, nothing plays when I run my game.

Can anyone help me out?

Here Mate

Instructions: 1 - attach this to any gameobject 2 - place the gameobject with the AudioSource component to mySource 3 - Lets say you have 3 audio clips you want to use, set both Range Scan and My Audio Elements to 3 4 - add your 3 clips as elements 5 - Enable Debug to get a list of the clips on the console but they also show on the editor as you play the game

Tips: this is on enable, so everytime this object is enable its gonna run a new range and assign the clip, Hope it helps took me about 5 minutes, if this helps you please consider following my facebook dev group HERE

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

public class randomSound : MonoBehaviour {

public AudioSource mySource;
public int rangeScan;
public AudioClip[] myAudio;
public int toPlay;
public bool debugging;

void OnEnable () {
    toPlay = Random.Range(0,rangeScan);
    if (debugging) {
        foreach (AudioClip value in myAudio) {
            print (value);
        }
    }
    mySource.PlayOneShot(myAudio[toPlay], 0.9F);
    mySource.Play ();
}

}