- Home /
Loading level after sound plays
Hi guys Still trying to get to grips with C# and im still not very good, im trying to have a sound play when a gui button is pressed before the loadlevel is executed. I've done the research and im still not getting it. So far the sound doesnt play and the level doesnt load either. I somehow got it to play the sound but the level didnt load. - here's what i got so far:
public class levelSelector : MonoBehaviour
{
public Texture levelOne;
public Texture levelTwo;
public Texture levelThree;
public GUIStyle GuiSlot;
IEnumerator playSoundThenLoad()
{
audio.Play();
yield return new WaitForSeconds(2);
Application.LoadLevel("level 1");
}
void OnGUI () {
if(GUI.Button(new Rect(270,110,300,100), levelOne, GuiSlot))
playSoundThenLoad(); // Problem here
}
audio.Play();
yield return new WaitForSeconds(audio.clip.length);
this would do it
I updated the code to :
IEnumerator playSoundThenLoad() { audio.Play();
yield return new WaitForSeconds(audio.clip.length);
Application.LoadLevel("level 1");
}
but that still didnt work - it didnt playh a sound or load the level
oh w8 you have to call the function like this
StartCoroutine(playSoundThenLoad());
Or even better, post this comment as answer (or convert the existing one) and mark it as answered. That way it will not appear in the 'unanswered' list and you'll also give/get karme. :3
Answer by silvereon · May 21, 2012 at 05:46 AM
this one's been answered by flamy:
IEnumerator playSoundThenLoad() { audio.Play();
yield return new WaitForSeconds(audio.clip.length);
Application.LoadLevel("level 1");
}
and call the coroutine manually:
StartCoroutine(playSoundThenLoad());
Your answer
![](https://koobas.hobune.stream/wayback/20220613071718im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Audio after button press before LoadLevel. 3 Answers
coroutine or yeild for OnGUI? 0 Answers
Fading out before load script not functioning correctly? 2 Answers
How to Make the Game Go to the Next Level When the Music Stops 2 Answers
Play Audio Through Scenes 0 Answers