Audio clip wont play on trigger enter

Hello, Im having trouble making it so when my player enters a area, It plays a audio clip. Im trying to create a horror game and sounds are a big part of it so I was hoping someone could edit my script or teach me how to do this because it isnt working… It says it is trying to play the audio clip but it cant find it I added the audio clip to the thing and I made sure its there in every way. Thank you for reading this and I hope you have a answer to my question

 var soundFile:AudioClip;
 private AudioSource _source;
 
[RequireComponent(typeof(AudioSource))] 
 function OnTriggerEnter(trigger:Collider) 
 {
        if(trigger.collider.tag == "Player")
        {
           _source.clip = soundFile;
           _source.Play();
        }
 }
 
 function Start () {
         _source = GetComponent<AudioSource>();
     }

Place this about your class name, just above ‘public class : MonoBehaviour’:
[RequireComponent(typeof(AudioSource))]
( this makes sure your script has a AudioSource :slight_smile: )

Create a variable private AudioSource _source; (I call my var _source)
Create a variable public AudioClip soundFile; (you already have this one)

In the start (or awake) method:
_source = GetComponent();

In your trigger:

function OnTriggerEnter(trigger:Collider) 
{
       if(trigger.collider.tag == "Player") 
       {
          _source.clip = soundFile;
          _source.Play();
       }
}

Good luck!


Edit:
This is the end result:

var soundFile:AudioClip;
    var source:AudioSource;
  
     function Start () {
            source= gameObject.GetComponent(AudioSource);
        }
  
    function OnTriggerEnter(trigger:Collider) 
    {
           if(trigger.collider.tag == "Player")
           {
              source.clip = soundFile;
              source.Play();
           }
    }