audio is not playing during IEnumerator
Hi, I am trying to play an audio clip in IEnumarator and after 10 seconds audio should stop. But when it comes to Play() method yes it is seem to be working, audio source take the clip and some sizzling sound can heard after ten seconds it stops with Stop() method. But the interesting part is when i commented out the Stop() method the audio can be heard after 10 seconds loud and clear. Anyone have any idea why this might be happening?
IEnumerator Chopping(float delay){
GetDeactive();
GameObject.Find("SoundManager").GetComponent<SoundManagement>().PlayChoppingSound();
yield return new WaitForSeconds(delay);
GameObject.Find("SoundManager").GetComponent<SoundManagement>().StopChoppingSound();
GetComponent<PathFinder>().MoveCharacterTo(GameObject.Find("TownCenter").transform.position);
AssignTarget(GameObject.Find("TownCenter"));
destination = Destination.townCenter;
}
SoundManagement Script;
public class SoundManagement : MonoBehaviour
{
AudioSource audioSource;
public AudioClip chopping;
// Start is called before the first frame update
void Start()
{
audioSource = GetComponent<AudioSource>();
}
public void PlayChoppingSound(){
audioSource.clip = chopping;
audioSource.Play();
}
public void StopChoppingSound(){
audioSource.clip = chopping;
audioSource.Stop();
}
}
Answer by ayseaktas · Mar 22, 2021 at 05:47 PM
I solved it. I was calling coroutine in Update method so it was being called in every frame and got crazy. I am calling it in Update method again but with bools and if-elses, i make sure it being called once.
Your answer
Follow this Question
Related Questions
Audio Fade In / Fade Out with Trigger 0 Answers
Upon hitting Play, my character creates a new Audio Source Component 0 Answers
Audio doesn't sound 0 Answers
Audio is cut in script 0 Answers
audio.play does not work 1 Answer