- Home /
Question by
KnightRiderGuy · Dec 12, 2015 at 04:28 PM ·
c#scene-loadingaudioclipcoroutinesaudio.playoneshot
Wait for audio to finish and then load scene
I keep forgetting how to do this.
using UnityEngine;
using System.Collections;
public class KittIDsounder : MonoBehaviour {
public AudioClip KittID;
// Use this for initialization
void Start () {
StartCoroutine(LoadKittID("MainScreen"));
}
IEnumerator LoadKittID(string level01){
yield return new WaitForSeconds(12.0f); // wait time
GetComponent<AudioSource>().PlayOneShot(KittID);
yield return new WaitForSeconds(AudioClip.length);
Application.LoadLevel(level01);
}
}
Comment
Best Answer
Answer by KnightRiderGuy · Dec 12, 2015 at 04:58 PM
This seems to do the trick.
using UnityEngine;
using System.Collections;
public class KittIDsounder : MonoBehaviour {
public AudioClip KittID;
// Use this for initialization
void Start () {
StartCoroutine(LoadKittID("MainScreen"));
}
IEnumerator LoadKittID(string level01){
yield return new WaitForSeconds(12.0f); // wait time
GetComponent<AudioSource>().PlayOneShot(KittID);
yield return new WaitForSeconds(10.0f);
Application.LoadLevel(level01);
}
}
Answer by Ambrose998800 · Dec 12, 2015 at 04:35 PM
Try with AudioSource.isPlaying(); As long your audio is playing, keep your script away from loading the scene. If it's done, toggle the last switch to let it load.
@Ambrose998800, I'm not sure I'm following you?
yield return new WaitForSeconds.AudioSource.isPlaying();
Application.LoadLevel(level01);