Audio effect on collision

Hi. I have two objects, a player and an enemy, and I have the following script attached to the enemy (as well as the sound effect).

void OnCollisionEnter2D(Collision2D coll){
     if (coll.gameObject.tag == "Player"){
        audio.Play();
 }
    else {
        audio.Stop();
    }
}

I want when the player hits the enemy, this sound effect to be played, but in my case the effect is played at the begining of the scene.

How can i fix this?

There is nothing wrong with the script although you don’t need to do audio.Stop(); in the else part.

If the audio is playing at the beginning then you’ve probably checked Play on Awake of the audio source.
Uncheck “Play On Awake” in the inspector window for the GameObject on which you have added the audio source.

Looks like you have Play on Awake on your audio source file checked. You need to uncheck it.

For more info: Audio Source