Ok here we go:
var vol : float;
var expSound : AudioClip[];
function OnCollisionEnter (other : Collision){
PlayExpSound ();
Destroy(gameObject);
}
function PlayExpSound (){
audio.clip = expSound[Random.Range (0, expSound.Length)];
audio.volume = vol;
audio.Play();
}
Here is an error : “Can not play a disabled audio source”
var IdleAudioObj : GameObject;
var waveAudioObj : GameObject;
function Update (){
if(enemy.length <= 0){
enemiesDead = true;
}
if (enemy.length > 0){
enemiesDead = false;
}
if(enemiesDead){
IdleAudioObj.audio.Play();
}
if (!enemiesDead){
IdleAudioObj.audio.Stop();
waveAudioObj.audio.Play();
}
}
Wont play anything at all…
Any ideas?
You cannot play a sound, then destroy the audio controller for that sound. That is an error.
Add this to the destroy:
Destroy(gameObject, audio.clip.length);
This will force it to play the sound then destroy the object.
If you had an audio controller that created sound objects. You could say something like:
AudioController.CreateSound(expSound[Random.Range (0, expSound.Length)], vol, position);
This would create an object , create a audiosource clip it, play it, then destroy it when the audio clip was done.
As far as the second part… do each of those have an audio source, and if so, do they have clips assigned?
You can use this to play a clip at the transform point. It creates an audio source, plays the clip, then cleans it up. Works well for explosions where an object is destroyed.