Audio after button press before LoadLevel.

Hey everyone,
so I’ve seen all the other posts about this subject, but it isn’t working for me.
This is in the OnGUI function :

if(GUI.Button(Rect(90, 180, 260, 60), "Start!"))

{
      LoadLevel();
} 

Then in LoadLevel() function, i have this :

function LoadLevel()

{
audio.Play();
yield WaitForSeconds(1.0);
Application.LoadLevel(2);

}

The problem is, the function is called on Awake and it loads Level(2) directly after a second when I start up the scene. How do I fix this?

Simple answer: call your function something other than LoadLevel(). :slight_smile:

Hope that helps, Klep

function OnGUI ()
{
if(GUI.Button(Rect(90, 180, 260, 60), “Start!”))
{
audio.Play();
if (!audio.isPlaying)
{
Application.LoadLevel (“1”);
}
}

hope this helps…:slight_smile:

OnGUI cannot be a coroutine.

function OnGUI()
{
    if(GUI.Button(Rect(90, 180, 260, 60), "Start!"))
    
    {
          PlayMusicThenLoadLevel();
    } 
}

function PlayMusicThenLoadLevel()
{
     audio.Play()
     yield;       //not sure this is needed
     while(audio.isPlaying) //if you want to wait until the end of the audioCLip before you load. Else, skip this.
     {
           yield;
     }
     Application.LoadLevel(yourLevelNb);

}