Detect a specific sound in a random play (beginner)

Hi all

as already told I’m a very beginner user. I’m now tring to do the following…
what I want is to play a random sound and I do it and it’s ok; but if I want unity to understand which sound he is playing I can’t do. I’ve tried with the code below…
thanks in advance to all “helpers / commenters” :smiley:

var aSounds  = new Array();
var gabbiano : AudioClip;
var ringer : AudioClip;


function Start() {
aSounds.Add(gabbiano);
aSounds.Add(ringer);
audio.PlayOneShot(sound[Random.Range(0, aSounds.lenght)]);

//Now here when the random sound is gabbiano should print CUBO OK but nothing :(
if (gabbiano.isPlaying) { 
print("CUBO OK");
}
}

There are several things wrong with this code:

1- isPlaying is an AudioSource property; gabbiano.isPlaying doesn’t exist;

2- PlayOneShot doesn’t affect audio.isPlaying - only Play does that.

2- You stored sounds in aSounds, but tried to play sounds from a sound array!

Anyway, you can’t tell if some AudioClip is playing, because only AudioSource has the isPlaying property. To do something similar to this, you should choose a random sound, store it in the audio.clip variable and call Play - then you could verify isPlaying, and print the sound name (its asset name, to be more precise).

Finally, you can use the builtin array instead of the Array() class:

var aSounds: AudioClip[]; // assign the sounds to this array in the Inspector

function Start(){
    audio.clip = aSounds[Random.Range(0, aSounds.length)];
    audio.Play();
}

function Update(){
  if (audio.isPlaying()) 
    print("Sound "+audio.clip.name+" is playing");
  else
    print("Sound ended");
}