Making a sound effect play a single time on collision

	void OnTriggerEnter2D(Collider2D collider)  {
		
		if (collider.tag == "ScoreBox") {
			audio.Play ();
}
}

Basically I want a sound effect to play when my player collides with a box collider. It’s working, however the sound continues to loop even though I have the sound effect set to not loop. The sound is in an audio source which is in the player, and I tried “PlayOneShot” but was fairly confused by it. So if that’s what I should be using, some explanation to it and how exactly I’d implement it in the code would be awesome. :slight_smile:

Thanks!

Would really love some help, I feel like it’s a simple solution and I’m just lacking the knowledge haha.

Try :

audio.PlayOneShot(YourSound);

You can either do as Intense_Gamer94 suggested before my reply, in which case you will need to specify your AudioClip on each call, or simply (within Start) set the following:

audio.loop = false;

… and then use your code as is.

I hope this helps.

Reference: AudioSource.loop

Thanks, works perfectly! If I wanted to do it the way Intense Gamer said however, how exactly do I specify the audio clip?

By that I mean what do I type and where?

By doing something like this :

//JS

var mySound : AudioClip;

function OnTriggerEnter2D(collider : Collider2D)  {

if (collider.tag == "ScoreBox") {

audio.PlayOneShot (mySound);

}
}

Nevermind I figured it out haha