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.
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;
}
}