Sound on tiggerExit not working anymore

Hi, I’ve a problem.

Since I upgrade to 3.5.6f4 to 4.5.1f3, my script doesn’t work anymore !
Th sound not playing at all…but the object destroy itself :confused:

Here my “script”

var clip: AudioClip;
   
   function OnTriggerExit() {
   audio.PlayOneShot(clip);
   Destroy( this.gameObject );
    }

What’s happening ?

Even with waitforsecond and even with destroy before audio.PlayOneShot…

Remove AudioSource from object, which you want to destroy, and attach him to another object, then replace

var clip: AudioClip;
var AnotherObject : GameObject;
 
   function OnTriggerExit() {
   AnotherObject.audio.PlayOneShot(clip);
   Destroy( this.gameObject );
    }

What you are doing is, within the same frame, you are playing the sound, then you are simply destroying the object which was going to play that sound, You need to destroy it AFTER it has finished playing.

I suggest using something like this:

var clip: AudioClip;
 
function OnTriggerExit()
{
   audio.PlayOneShot(clip);
   Invoke("destroyOnSoundFinished", clip.length);
}

function destroyOnSoundFinished()
{
   Destroy( this.gameObject );
}

Ok, I figured out !

Just delete the collider on triggerexit. :wink:
Thanks to all !

var clip: AudioClip;

function OnTriggerExit()
	{
 if(!audio.IsPlaying)
{
   audio.PlayOneShot(clip);
    Destroy(collider);
   Invoke("destroyOnSoundFinished", clip.length);
	}
}

function destroyOnSoundFinished()
{
   Destroy( this.gameObject );
}