Question by
alanbo24 · Jan 25, 2021 at 01:07 AM ·
startcoroutinesound effects
Unity Crashes after Timeline and at the start of a script
Hi ! Im pretty new to Unity 2Dand i just finished a Timeline to use it like a Cutscene and after this cutscene an enemy starts moving towards the player but i wanted it to play a sound effect in sort of a loop in random time. I tried modifying the script of the enemy adding a StartCoroutine. When the cutscene finish and where supposedly the sound effect should start playing, Unity crash and no idea what is the problem. When the StartCoroutine code is commented, everything is working perfectly.
This is my code.
public class PerroM : MonoBehaviour {
public GameObject perro;
public float velocidad = 0.0003f;
private bool stillMoving = false;
private bool audioPlaying = false;
private void FixedUpdate()
{
if(perro.transform.position.x < 44.5)
{
perro.transform.position += new Vector3(velocidad,0,0);
stillMoving = true;
} else
{
stillMoving = false;
}
while (stillMoving)
{
if (audioPlaying == false)
{
StartCoroutine(Bark());
}
}
}
IEnumerator Bark()
{
AudioSource audio = GetComponent<AudioSource>();
audio.Play();
audioPlaying = true;
yield return new WaitForSeconds(audio.clip.length);
yield return new WaitForSeconds(Random.Range(1, 5));
audioPlaying = false;
}
}
Comment