IEnumerator issues

I’m having issues with using IEnumerators, more specifically WaitForSeconds.
So I have this script that’s supposed to play a bgm into, wait until it’s done, then play the music loop:

public IEnumerator playMusic()
{
	audio.Play ();
	yield return new WaitForSeconds(audio.clip.length);
	audio.clip = musicLoop;
	audio.loop = true;
	audio.Play ();
}

But what’s going on instead is it doesn’t play the loop until after twice the length of the intro.

I’ve had this problem before with stopping the music, playing a jingle, and resuming the music.

Here is the full script if it’ll be any help:

using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class AudioDirector : MonoBehaviour {
	public AudioClip musicIntro;
	public AudioClip musicLoop;
	GameObject musicSource;
	public AudioClip[] Jingles;
	public int JingleType;

	// Use this for initialization
	void Start () {
		musicSource = gameObject;
		audio.clip = musicIntro;
		playMusic ();
	
	}
	
	// Update is called once per frame
	void Update () {
		DontDestroyOnLoad (musicSource);
	}
	public IEnumerator playMusic()
	{
		audio.Play ();
		yield return new WaitForSeconds(audio.clip.length);
		audio.clip = musicLoop;
		audio.loop = true;
		audio.Play ();
	}
	public IEnumerator PlayJingle(int jingletype)
	{
		JingleType = jingletype;
		audio.Stop ();
		audio.loop = false;
		audio.clip = Jingles [JingleType];
		audio.Play ();
		yield return new WaitForSeconds(audio.clip.length);
		audio.clip = musicLoop;
		audio.loop = true;
		audio.Play ();
	}
}

I’m surprised it waited at all.

In C# you need to use StartCouroutine

You should be starting a coroutine, passing the IEnumerator into that method.

So instead of:

playMusic();

you should be calling:

StartCoroutine(playMusic());

An IEnumerator is nothing magical, and as such, must be “registered” within Unity (by passing it into StartCoroutine), this will make sure Unity will periodically run it, and also let you yield from it and return after parts of it are complete (e.g: wait for some time, etc).

Well, what you first should check is:

  • What’s the actual length of your intro outside of Unity?
  • What does “musicIntro.length” report?
  • Try starting the playMusic coroutine manually for testing and measure the actual time it takes.
  • Also it makes no sense to call DontDestroyOnLoad every frame. If you call it once it will never be destroyed automatically. The only way is using Destroy(). Keep in mind that DontDestroyOnLoad only works on root objects which don’t have a parent. If a child is marked with DontDestroyOnLoad but the parent is not, it’s useless.

You can do something like that:

void Start ()
{
    musicSource = gameObject;
    DontDestroyOnLoad (musicSource); 
}
 
public IEnumerator playMusic()
{
    Debug.Log("Intro length: " + musicIntro.length);
    var startTime = Time.realtimeSinceStartup;

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

    Debug.Log("audio.clip.length : " + audio.clip.length);

    yield return new WaitForSeconds(audio.clip.length);
    
    var timeTaken = Time.realtimeSinceStartup - startTime;
    Debug.Log("time taken: " + timeTaken);
    
    audio.clip = musicLoop;
    audio.loop = true;
    audio.Play ();
}


void OnGUI()
{
   if (GUI.Button(new Rect(10,10,100,40), "Start BGM"))
   {
       StartCoroutine(playMusic());
   }
}