one shot Sound on collision. Unity 4.0 problem.

Im trying to do so, if you for example go in to a room i want a sound to start, so I put a cube ,with “is trigger” checked, to the player to collide with and then the gameobject(cube) destroys so it doesnt play a second time when entering the room. I attach the script to the cube and ofcourse sets the players tag to “Player”. I have always used this script but now, in 4.0 it doesnt work. I think the gameobject destroys before the sounds begin playing so you cant hear the sound

#Script

 var sound : AudioClip;
    
    function OnTriggerEnter( hit : Collider )
    	{
    
    	if(hit.gameObject.tag == "Player")
    		{
    		Destroy(gameObject);
    		audio.PlayOneShot(sound);
    		}
    
    	}

You need to destroy the object after a delay. You can do this by calling Invoke and passing in the time. The time should be longer than it takes to play the sound.

So…

...
{
  audio.PlayOneShot(sound);
  Invoke(DestroySelf, 3)
}

void DestorySelf()
{
  Destroy(gameObject);
}

Destroy() has a time argument, this is the cleanest to do it:

function OnTriggerEnter( hit : Collider )
{
   if(hit.gameObject.tag == "Player")
     {
        if (!alreadyPlayed)
        {
           audio.PlayOneShot(sound);
           alreadyPlayed = true;
           Destroy(gameObject, 5);
        }
     }
}