How can I add a script that plays a sudden sound once you see an object?

How can I implement a script that once the character see’s the object, a sudden sound plays like in the game “Slender”. Once you see him, there is a sudden sound that plays.

The way my scene goes is when the player unlocks a door, there will be a bloody mattress in the other room and I want the sudden audio to scare the player as soon as he see’s the bloody mattress.

Here’s my script hat i’ve used but keep replaying sound each time I look at it. I only want it played once.

using UnityEngine;
using System.Collections;

public class Jumpscare : MonoBehaviour {

    public GameObject popup;
    RaycastHit raycast;

    void Update () {
    Physics.Raycast(transform.position,transform.forward, out raycast);
       if(raycast.transform.gameObject == popup)
       {
         popup.audio.Play();
			//Destroy (gameObject, 1);
       }

    }
}

The basic idea is ok - just use a boolean flag to avoid repeating:

bool scream = true;

void Update(){
  if (scream){
    Physics.Raycast(transform.position,transform.forward, out raycast);
    if (raycast.transform.gameObject == popup) // if popup at sight...
    {
      popup.audio.Play(); // scream once!
      scream = false; // and don't scream anymore
    }
  }
}

being a total newb i’d add a var soundPlayed = false, then asked if sound was already played before playing it and change it’s value to true right where you have //Destroy (gameObject, 1);

You could do a few things, one being have it pay in the startup method. Or you could simply flip a Boolean after you played it once that way it wont play it again.