I want to make my audio play only once

I have a fence in my game and if my player collides with the fence, the audio of opening fence will be called. After that if my player collides with my fence, the audio will be played again. I don’t want to make my audio play more than once. By the way my program is this.

[31619-screen+shot+2014-08-27+at+21.25.11.png|31619]

You mean OnCollisionEnter gets called twice? If that’s the case, I’m not sure what it is but here’s a work-around :

// If audio isn't already playing, play it.
if(!audio.isPlaying)
{
    audio.Play();
}

One way to accomplish this would be to simply have a boolean variable that gets checked before playing the sound and set when the sound plays. Something like this:

bool soundHasPlayed = false;

function OnCollisionEnter(){
	if (!soundHasPlayed){
		audio.Play();
		soundHasPlayed = true;
	}
}