I’m having this problem.
Earlier today, I had an intro/loop thing set up where the “director” has an array of all the music in the game and the level tells it what music to play. It was working perfectly fine until I remade one of the level meshes and reattached the script that tells the director what to play.
What it’s doing, instead of playing the loop as soon as the intro is over, is it waits a specific amount of time (I’ve figured out it’s the length of the very first intro in the Intros array) and starts the loop.
For some songs, it plays the intro, has a moment of silence, and then plays the loop. Others, it plays the intro and starts the loop and then restarts the loop after a few seconds.
I don’t know exactly how changing a level mesh would do this (considering I didn’t edit any of the scripts since I got them working).
If it’s any help, here are the scripts:
AudioDirector: Attached to the director
using UnityEngine;
using System.Collections;
[RequireComponent(typeof(AudioSource))]
public class AudioDirector : MonoBehaviour {
public AudioClip[] musicIntros;
public AudioClip[] musicLoops;
public int MusicIndex;
//public GameObject musicSource;
public AudioClip[] Jingles;
public int JingleType;
// Use this for initialization
void Start () {
//musicSource = gameObject;
StartCoroutine(playMusic ());
}
// Update is called once per frame
void Update () {
//DontDestroyOnLoad (musicSource);
}
public IEnumerator playMusic()
{
audio.clip = musicIntros[MusicIndex];
audio.Play ();
yield return new WaitForSeconds(audio.clip.length);
audio.clip = musicLoops[MusicIndex];
audio.loop = true;
audio.Play ();
}
public IEnumerator PlayJingle(int jingletype)
{
JingleType = jingletype;
audio.Pause ();
audio.loop = false;
audio.clip = Jingles [JingleType];
audio.Play ();
yield return new WaitForSeconds(audio.clip.length);
audio.clip = musicLoops[MusicIndex];
audio.loop = true;
audio.Play ();
}
}
ChangeLevelMusic: Attached to the level
using UnityEngine;
using System.Collections;
public class ChangeLevelMusic : MonoBehaviour {
GameObject director;
public int ThisLevelMusicNumber;
//public AudioClip ThisLevelIntro;
//public AudioClip ThisLevelLoop;
// Use this for initialization
void Start () {
director = GameObject.FindGameObjectWithTag ("Director");
director.audio.Stop ();
director.GetComponent<AudioDirector> ().MusicIndex = ThisLevelMusicNumber;
StartCoroutine(director.GetComponent<AudioDirector> ().playMusic ());
}
// Update is called once per frame
void Update () {
//director.GetComponent<AudioDirector> ().musicIndex = ThisLevelMusicNumber;
}
}
EDIT: It doesn’t wait the length of the first intro in the array. Apparently the only song to work correctly is the first one.
None of the other ones work correctly.
Also earlier one of the loops didn’t loop.