Play audio for x seconds and pause for x seconds?

Hi, I think ive just set out on a bad foot with this one and over complicated things.

I have a short audio file that is a single monotone and I have it repeat so its like a ongoing “tone”.

However I want it to act like a “beep” so either pauses for n seconds or set the volume to 0 for n seconds either one is fine. However i want to to STOP for n seconds and then PLAY for n seconds and so on, n is variable which changes?

Any pointers?

Thanks

Hi, have you considered using a coroutine ?. You could pass ‘n’ and use yieldreturnnewWaitForSeconds(n).

Like Songroom said, a coroutine would get the job done.
Do something like this:

public float playTime = 0.5f;
public float pauseTime = 0.5f;
private AudioSource beep;

void Start()
{
    beep = GetComponent<AudioSource>();
    StartCoroutine("PlayPauseCoroutine");
}

IEnumerator PlayPauseCoroutine()
{
    while(true)
    {
        beep.Play();
        yield return new WaitForSeconds(playTime);
        beep.Pause(); // or beep.Stop()
        yield return new WaitForSeconds(pauseTime);
    }
}