#pragma strict
var sound : AudioClip;
function OnGUI() {
if (GUI.Button(Rect(600,400,100,100),"Play") ){
audio.PlayOneShot (sound, 1);
yield return new WaitForSeconds(2);
Application.LoadLevel ("spacescene");
}
}
#pragma strict
var sound : AudioClip;
function OnGUI() {
if (GUI.Button(Rect(600,400,100,100),"Play") ){
audio.PlayOneShot (sound, 1);
yield return new WaitForSeconds(2);
Application.LoadLevel ("spacescene");
}
}
OnGUI really shouldn’t be run as a coroutine. For OnGUI to work it must run the code every frame to display the button.
To achieve the same effect you should have the button call StartCoroutine on a separate method.
Yes OnGUI cannot be used as a coroutine.Since in a drawing function,it should be updated every frame.You can achieve what you are looking by starting another coroutine from the OnGUI. Something like this
#pragma strict
var sound : AudioClip;
function OnGUI()
{
if (GUI.Button(Rect(600,400,100,100),"Play"))
{
//Play the audioclip
audio.PlayOneShot(sound,1);
//Start the coroutine
StartCoroutine(WaitToLoad());
}
}
//The coroutine function to load with delay
function WaitToLoad()
{
//Wait for 2 seconds
yield WaitForSeconds(2.0);
//Load the next scene
Application.LoadLevel ("spacescene");
}