x


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.

more ▼

asked Dec 22 '11 at 07:28 AM

venkspower gravatar image

venkspower
284 17 24 27

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Dec 22 '11 at 07:50 AM

syclamoth gravatar image

syclamoth
14.8k 7 15 80

Hey, thanks. Silly me. It worked.

Dec 22 '11 at 10:10 AM venkspower
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5096
x1031
x171
x126

asked: Dec 22 '11 at 07:28 AM

Seen: 1148 times

Last Updated: Dec 22 '11 at 10:48 AM