Setting an AudioClip with code?

Hey guys,

I’ve got 200+ sounds that need to be loaded (one at a time only) in an arbitrary order. Is there any way I can just utilise one audio clip and set it with code?

Example…

sound_1_1.wav
… all the way to…
sound_1_203.wav

Is there anyway I can use sfx.AudioClip.clip = “” or something?

Not sure if this is exactly what you’re looking for, but here’s a script I use to play a set of sounds in random order with random intervals, using the same AudioSource. With a little modification, the clip array could be loaded from Resources instead of dragged-and-dropped into the script (I do that in variations of this script, but they’re all in messy shape)

var clips : AudioClip[];
var minInt = 10;
var maxInt = 30;

function Start ()
{
PlaySongs ();
}

function PickRandomSong () : AudioClip
{
var index : int= Random.Range(0, clips.length);
return clips[index];
}

function PlaySongs ()
{
while (true)
{
var clip : AudioClip = PickRandomSong ();
if (clip != null)
{
audio.clip = clip;
audio.Play();
yield WaitForSeconds(clip.length);
yield WaitForSeconds(Random.Range(minInt,maxInt));
}
yield;
}
}

@script RequireComponent(AudioSource)

[/code]

Loading it from resources is the bit I need, but straight away you’ve already suggested using an array, which… I have no idea why i hadn’t even considered that :slight_smile:

I guess the script just needs one line in the Start function, something like:

clips = Resources.LoadAll("Songs",AudioClip);

substituting the Resource subdirectory name you want to use. I should add that to my own script and save myself some additional drag-and-drops.

Resources.Load(“blah”,AudioClip) !

Horay! I didn’t even know where to look :stuck_out_tongue: