How to correctly stop audio after one play

Hello forumers,

I have just started playing around with an audio manager in Unity and am still very new to scripting for playing audio files.
The problem I am having currently is that my Audio sound continues to repeat itself before it can even end. This is because I am putting it inside update, although I need it to play once when my raycast collides with an object. I am not sure which function is best for this type of encounter. Ideally, I would like for the sound to play when my ray first collides, and doesnt play again unless my ray goes off the object and back on.

I just have no idea how I would begin this.

Thank you for helping,

You could always store a private GameObject variable, and on Update, store your Raycast-hit object to the gameobject. If the object changes and is not null, play the sound. This would result in one play while looking at it, none when not looking at it, and would play again when looking back at it.

So…

private GameObject rayObject;

Update(){
    object = Raycast....
    if(object != null && rayObject != object){
        playSound();
    }
    rayObject = object;
}

Of course, my best guess in semi-pseudocode!

To play audio one time without repeating you should use this: