Adding Audio clip in scripting

I am adding audio files to my object in my script. I have done:-

var voice_1 : AudioSource;
if(currentWaypoint==1)
{
     audio.clip = voice_1;
     audio.Play();
}

I have dragged the Audio File which I have recorded to the inspector. But it is giving a compiler error as: “Cannot convert UnityEngine.AudoSource to UnityEngine.AudioClip.” How to play it through the script. Help! Thanks in advance.

Well, it’s pretty obvious that you have to make voice_1 an AudioClip, not an AudioSource!

More importantly, once you’ve done that, you can’t just use the ‘Play’ method with an AudioClip. To play a clip at a point, you can use

AudioSource.PlayClipAtPoint(voice_1, transform.position);

Which will play a one-shot of that audio clip at the location of the object.

Basically, you need to change this line-

var voice_1 : AudioSource;

to this

var voice_1 : AudioClip;

and it’ll all work. How you use it is up to you, though.