- Home /
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.
Answer by syclamoth · Dec 22, 2011 at 07:50 AM
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.
Your answer
Follow this Question
Related Questions
Unity Unable to Reassign Audio Clip 1 Answer
C sharp cant play Audio 2 Answers
Audiosource.Play() not working 0 Answers
Unity crashing on audio but not in editor? 0 Answers
How Do I Get This To Play Sound? 0 Answers