Why doesn't my sound script work? (C#)

Hey there,
I’m trying to create a sound library for my game, which plays one song after the other. Here is my code:

using UnityEngine;
using System.Collections;

public class GameSound : MonoBehaviour {
	public AudioClip FirstTrack;
	public AudioClip SecondTrack;
	void Awake () {
	DontDestroyOnLoad(this);
	FirstSong();
	}
	
	
 	IEnumerator FirstSong() {
    audio.clip = FirstTrack;
	audio.Play();
    yield return new WaitForSeconds(FirstTrack.length);
	SecondSong();
    }
	IEnumerator SecondSong() {
    audio.clip = SecondTrack;
	audio.Play();
    yield return new WaitForSeconds(audio.clip.length);
	
    }
}

I did use the unity reference guide. But it doesn’t work still, can someone please tell me why?

1 Answer

1

When you are calling IEnumerator function in c#, you should call it like:

StartCoroutine (FirstSong());

/* and another */

StartCoroutine ("SecondSong");

Ah thank you, i completely forgot about coroutines haha :)