Wait some time till previous audio plays

I am new Unity.I have achieve one small issue.
I am playing one audio file as
It is not solved my problem.
my code is like as

        ShowAppreciation ();	 
   FlashCardsManager.instance.PlayTappingAudio ();

    ShowAppreciation {
    audio.clip = appreciationClip;
    audio.PlayOneShot (appreciationClip);
      }

Here “FlashCardsManager” is another script file.While running this code both audios are playing simultaniously.
But I have to play second audio after completion of previous.So i have to wait 5-10 seconds.

Any one help me on this to wait some time

I tried this code.

yield WaitForSeconds(3);  

but it’s not working

2 Answers

2

You should read up in Coroutines. Here is the Unity tutorial. You can use yield within a coroutine to determine at what points the coroutine execution will pause and be resumed the next frame.

You could do something like this:

[SerializeField]
private AudioSource audioSource;
[SerializeField]
private AudioClip appreciationClip;
[SerializeField]
private AudioClip afterAppreciationClip;

void PlayAudio() {
    StartCoroutine(PlayAudioClip());
}

IEnumerator PlayAudioClip() {
    audioSource.clip = appreciationClip;
    audioSource.Play();
    while (audioSource.isPlaying) {
        // do nothing and keep returning while audio is still playing
        yield return null;
    }

    // wait a bit before playing next clip
    yield return new WaitForSeconds(5.0f);

    audioSource.clip = afterAppreciationClip;
    audioSource.Play();
}

Try this:

audio.clip = appreciationClip;
audio.Play (appreciationClip);

and in Update:

if(!audio.IsPlaying() && audio.clip == appreciationClip)
{
    audio.clip = null; 
    // or audio.clip = otherclip
    //audio.Play();
}

Yield is used in coroutines. Take a look at this article:
Unity gems - coroutines