I am making a system where I have one long audio clip made up of voice lines which I want to be able to play one after the other with a variable pause in between.
I press the space key to start the clip, and I want it to pause after a set time and then I will press the enter key to start the next line(s).
I am able to get the audio to pause inside Update()
with AudioSource.Pause();
but for some reason it will not work inside the coroutine I have set to pause it after a number of seconds.
Since I do not want to have to time the pauses myself, how can I achieve this? The debug messages inside the SoundWaiter()
function are all printing, but the audio is not being paused.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class SoundTracker : MonoBehaviour
{
public AudioSource soundFX;
public bool pausing = false;
public int count = 0;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Return))
{
pausing = !pausing;
}
if(pausing)
{
// This works, but I don't want to have to time the pauses myself
//soundFX.Pause();
}
if (!pausing)
{
soundFX.UnPause();
}
if (Input.GetKeyDown(KeyCode.Space))
{
if (count == 0)
{
soundFX.Play();
StartCoroutine(SoundWaiter(10));
}
//if (count == 1)
//{
//soundFX.UnPause();
//StartCoroutine(SoundWaiter(5));
//}
//if (count == 2)
//{
//soundFX.UnPause();
//StartCoroutine(SoundWaiter(12));
//}
//count = count + 1;
}
}
IEnumerator SoundWaiter(float duration)
{
Debug.Log("In waiter...");
yield return new WaitForSeconds(duration);
Debug.Log("Pausing audio...");
soundFX.Pause();
Debug.Log("Paused audio.");
}
}
```