- Home /
audio source not playing the audio clip
Newbie here! I have an animator prefab that gets instantiated to which an audio source is added which eventually have an audio clip. I have a script which would play the clip when the prefab is clicked as follows.
public class destroy : MonoBehaviour {
void Start () {
AudioSource audio = GetComponent<AudioSource>();
}
void Update () {
}
void OnMouseDown(){
audio.Play ();
Destroy(this.gameObject);
}
}
for some reason my clip is not being played. The OnMouseDown() is working though. Please help me. Thanks in alvance :)
Try changing the settings of the audio clip to 2D sound. A 3D sound will not be heard if too far away from the audio listener.
Answer by zach-r-d · Jun 10, 2015 at 06:52 PM
It looks like the GameObject with the AudioSource on it is being destroyed as soon as Play() is called, so the AudioSource is destroyed before it can finish (or perhaps even start) playing. Try putting the audio source on a different object that isn't destroyed until after the clip is finished playing.
Answer by vmexia · May 30, 2016 at 02:48 PM
@abhishekabz004 You should use animation events. With this so you can call a function, in your case to destroy the object, at the end of the animation.