- Home /
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?
Answer by Kleptomaniac · Apr 07, 2012 at 08:20 AM
Simple answer: call your function something other than LoadLevel(). :)
Hope that helps, Klep
Answer by Dee Va · Apr 07, 2012 at 08:32 AM
function OnGUI ()
{
if(GUI.Button(Rect(90, 180, 260, 60), "Start!"))
{
audio.Play();
if (!audio.isPlaying)
{
Application.LoadLevel ("1");
}
}
hope this helps........:)
Answer by gregzo · Apr 09, 2012 at 09:41 PM
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);
}
OnGUI wasnt a coroutine in the first place, and I tried your code, but Play$$anonymous$$usicThenLoadLevel() is still called when you start the scene. Any other ideas?
I'm a bit puzzled... Why would Play$$anonymous$$usicThenLoadLevel() be called if you don't press the button? Function's don't just randomly get called in Awake()... And about my "OnGUI cannot be a coroutine" comment, my bad.
i know right, its weird, i tried a bunch of different arrangements, like adding a bool thats starts off false and goes true when you press the button, but that didnt seem to work too great...
You must be calling the function from somewhere. As I said, functions don't just randomly get called for no reason... Did you try a debug in the function? $$anonymous$$ake sure it is really what is happening? Check all your scripts for a forgotten call somewhere?
Your answer
Follow this Question
Related Questions
Loading level after sound plays 1 Answer
coroutine or yeild for OnGUI? 0 Answers
How do I play a random sound each 5 seconds? 1 Answer
How to Make the Game Go to the Next Level When the Music Stops 2 Answers
Issues with WWW resource loading 1 Answer