- Home /
yield WaitForSeconds makes GUI button disappear
I'm showing a button, and when clicked, it plays an audio file. It should wait until the audio finishes playing and then delete another object. But, when I include the yield WaitForSeconds line, the GUI button fails to display.
var audioToPlay1:AudioClip;
function OnGUI() {
if (GUI.Button(Rect(50,100,300,30),"Play the audio")) {
audio.clip = audioToPlay1;
audio.Play();
// THE FOLLOWING LINE MAKES THE BUTTON DISAPPEAR
yield WaitForSeconds (audio.clip.length);
var objectToClear = GameObject.Find("collision_object");
Destroy(objectToClear);
}
}
Is there any reason why the inclusion of yield WaitForSeconds would cause the GUI button to disappear? Thanks in advance.
Answer by Eric5h5 · Aug 16, 2011 at 03:42 AM
You can't make OnGUI into a coroutine; it runs every frame and can't be paused or interrupted in any way. You can move the button code into a separate function and have that be a coroutine instead.
Eric5h5, thank you very much. That answered it perfectly. I now realize that OnGUI works the same way as Update, in that it runs every frame and can't be paused. Putting my "action" code into a separate function worked perfectly.
Your answer
Follow this Question
Related Questions
Putting a delay in a button OnClick ? (Javascript) 3 Answers
Loop animation with a wait in between 3 Answers
Infinite GUI Button positions. 3 Answers
Making a Hover function for a button or toolbar from code. 2 Answers
Bool script error 1 Answer