Audio

So the following plays audio when my ammo is picked up, the only problem is, when I destroy the game object, the audio is not played, is there anyway to play the audio and destroy the game object at the same time?

function OnTriggerEnter(hit : Collider)
{
   if (hit.transform.tag == "Player") {
      hit.gameObject.BroadcastMessage("addAmmo");
      audio.Play();
      Destroy(gameObject);
   }
}

Put the Audio Source on an empty gameobject and link the two.

but wont this leave loads of empty game objects round my scene? Also, how do I link them?

Don’t make empty game objects, use PlayClipAtPoint. (Actually this is the same general concept, but it does everything including cleanup for you.)

–Eric

That’s why you have a cached array of AudioClips and switch the main clip of the AudioSource before you hit play.

Heh, I guess I’m just used to using a master AudioSource for all sounds that don’t need to be directional.