Make Array Of All Objects In A Folder

I am making a script to manage background music in my game, which will find all the songs in my music folder and play them at random. What I am using at the moment is:

function Start () {
var allsongs : Object[]=AssetDatabase.LoadAllAssetsAtPath ("Assets/Music");
for(var song : Object in music){
PickWhatToPlay();
}
}

but it doesn’t work. No errors, but when I get it to print the list nothing happens.
Please could someone tell me what is going wrong?

I’m assuming that this is a script that you intend to use in your (eg: it’s NOT an Editor script). The AssetDatabase is in the UnityEditor namespace, so it’s fine to use it in the Editor, but when you try to build your game, you’ll get build errors.

To load the music files at run-time, you should instead move your Assets/Music directory into a Resources directory, for example, Assets/Resources/Music, and then load it using Resources.LoadAll

function Start () {
  var allsongs : Object[]=Resources.LoadAll("Assets/Music", AudioClip);
  for(var song : Object in music){
    PickWhatToPlay();
  }
}

I haven’t tested this code, but it should be enough to get you started.