Making a sound file play when AI sees you.

Hi there, I am trying to make a sound file play when the AI sees you, But its not working(probably because I am very new to coding) could you guys take a look at my code and tell me what I am doing wrong and or correct it for me? Thanks

Here is the code for the AI

var distance;
    var target : Transform;    
    var lookAtDistance = 15.0;
    var attackRange = 10.0;
    var moveSpeed = 5.0;
    var damping = 6.0;
    var Lookat : AudioClip;
    private var isItAttacking = false;
 
    function Update () 
    {
    distance = Vector3.Distance(target.position, transform.position);
 
    if(distance < lookAtDistance)
    {
    isItAttacking = false;
    renderer.material.color = Color.yellow;
    lookAt ();
    }   
    if(distance > lookAtDistance)
    {
    renderer.material.color = Color.green; 
    }
    if(distance < attackRange)
    {
    attack ();
    }
    if(isItAttacking)
    {
    renderer.material.color = Color.red;
    }
}
 
 
function lookAt ()
{
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
}
 
function attack ()
{
    isItAttacking = true;
    renderer.material.color = Color.red;
 
    transform.Translate(Vector3.forward * moveSpeed *Time.deltaTime);
}


function PlayAudio() 
{
    if ( lookAtDistance = true )
    {
       if ( audio.clip != lookAt )
       {
         audio.Stop();
         audio.clip = lookAt;
       }
 
       if ( !audio.isPlaying )
       {
         audio.Play();
       }
    }
     else
    {
       audio.Stop();
    }
}

Your function PlayAudio is not called in this script. It is defined but never used.

Also, there is something off in the beginning of your PlayAudio() function. In if ( lookAtDistance = true ) you are using the assignment operator = rather than the comparison operator ==. So that will always evaluate to true and you are assigning lookAtDistance to 1. You probably want to check something other than true since this is set to 15. It may help to pause the game while playing and look at the current value of your public variables in the editor. Also using Debug.log() to check if certain code is being executed.

This answer about playing audio clips might help too:

Since you are using Javascript, I also highly recommend putting #pragma strict at the top of your script, which will give you better errors/warnings.