Check Distance and Play Audio

I’m creating a game where the character is in a forest and needs to be led to a barn…
In order for the character to be led to the barn, I was hoping to calculate the distance between the character and the barn

  • if distance is > x, it will play an audio piece…
    • if distance is < x, it will play a different audio piece…

However, with this… it constantly keeps calculating the distance and therefore playing the sound every step (playing start of audio, then replaying start and so on…) - audio does not finish and keeps repeating.

Is there a way I can calculate the distance, store the calculation - play sound, then re-start calculation etc;

Here is what I have so far… any help would be appreciated.

#pragma strict
 var thePlayer : Transform;
 var Barn: Transform;
var hot: AudioClip;
var cold: AudioClip;
 function Start(){

 function Update(){
  var BarnDistance = Vector3.Distance(thePlayer.position, Barn.position);
 if(rigidbody.velocity.magnitude == 0);

 if ( BarnDistance > 100) {
            audio.clip = cold;
  {  if ( BarnDistance < 100) {
            audio.clip = hot;

The reason is because it’s using the Update() function. This will cause the evaluation to become true every frame, causing your audio to play every frame. You should instead create your own method and refer to it on the occasions your conditions are met. If you are playing only 2 different songs, I would suggest a simple bool.