- Home /
Trigger audio clip on load level async (only playing for a fraction of a second)
Hello,
I want to play an audio clip once I set a level to load (using loadlevelasync), but am struggling to get this to work.
I'm checking to see if a bool is set to true in the loadlevelasync script: at which point I want the audio clip to play.
However - I'm having the problem that it's playing for a fraction of a second once the bool is set to true. Then you can't hear the audio clip, and then once the new level is loaded you can hear the clip.
If I call the audio clip from a key press and then load the level at the same time, then I can hear the audio clip whilst the async load is happening, so I don't think it's because it is freezing. (And other audio clips are still playing).
Does anyone know what I might be doing wrong? (Or if I should go about this in a different way?)
using UnityEngine;
using System.Collections;
public class DontDestroyLoad : MonoBehaviour {
public GameObject[] musicSwipe;
GameObject levelLoaderObject;
void Awake(){
DontDestroyOnLoad (this.gameObject);
}
void Start ()
{
musicSwipe= GameObject.FindGameObjectsWithTag ("AudioSwipe");
}
void Update()
{
if (Application.loadedLevelName == "Title")
{
levelLoaderObject = GameObject.FindGameObjectWithTag ("LevelLoader");
if (levelLoaderObject.GetComponent<AsyncLoad>().levelTrue == true)
{
print ("playing?");
StartCoroutine(PlaySwipe());
}
}
}
IEnumerator PlaySwipe () {
if (musicSwipe.Length == 1 )
{
GetComponent<AudioSource>().Play ();
} else {
for(int i = 1; i < musicSwipe.Length; i++){
Destroy(musicSwipe[i]);
}
}
yield return null;
}
}
Best, Laurien