- Home /
yield WaitForSeconds problem?
Hello everyone.
I've create a 'yield WaitForSeconds()' and I want a message to change after specific seconds. My problem is that in the first 'yield WaitForSeconds()' I set the seconds to 2 for example, and on the second 'yield WaitForSeconds()' I set the seconds to 1. The problem is that it only reads the first 'yield WaitForSeconds()' and then it skips it all. Here is my code, please help me :)
function second_textGUI(){
first_text_message = "(Sshhh!!)";
yield WaitForSeconds(3);
first_text_message = "(Hmm.. who is he?)";
yield WaitForSeconds(2);
first_text_message = "(He might be the murderer!!!)";
yield WaitForSeconds(3);
first_text_message = "";
second_en = false;
}
Any help is appriciated :)
Edit: Is there any other way similar to yield WaitForSeconds()
?
Do you mean the coroutine doesn't continue executing after the first yield WaitForSeconds call? Have you added Debug.Logs to confirm what gets executed? How is first_text_message declared and where is it used?
Initially I though your were declaring the function wrong, then I noticed it was UnityScript not C# :)
Are you calling second_textGUI() multiple times in either Update() or OnGUI()?
@robertbu I call it only in OnGUI() function.
@Huacanacha I've checked it with Debug.Log, but it works only if all the yield WaitForSeconds have the SA$$anonymous$$E seconds.. If I check atleast on yield WaitForSeconds then it going to mesh up.. :(
@liszto I get no erros.. but it's not working properly
Is it called once during an event (a button pressed) or inside the GUI loop (several times per frame)
Answer by ardizzle · Nov 11, 2013 at 01:08 PM
Hey man I remember having the same issue a while back. In the end I just made a work around that ended up looking like this
var firstWait : boolean = true;
var secondWait : boolean = false;
function second_textGUI()
{
if(firstWait == true)
{
first_text_message = "(Sshhh!!)";
yield WaitForSeconds(3);
first_text_message = "(Hmm.. who is he?)";
firstWait = false;
}
if(firstWait == false && secondWait == true)
{
yield WaitForSeconds(2);
first_text_message = "(He might be the murderer!!!)";
secondWait = false;
}
if(secondWait == false)
{
yield WaitForSeconds(3);
first_text_message = "";
second_en = false;
//firstWait = true; undo these comments if you want it to loop
//secondWait = true;
}
}
really ?! and this works better than the other ?!
IENumerator second_textGUI(){
first_text_message = "(Sshhh!!)";
yield return new WaitForSeconds(3);
first_text_message = "(Hmm.. who is he?)";
yield return new WaitForSeconds(2);
first_text_message = "(He might be the murderer!!!)";
yield return new WaitForSeconds(3);
first_text_message = "";
second_en = false;
}
I can just help you in C# but I think it's weird that your code part won't work.
Answer by static_cast · Nov 11, 2013 at 03:32 PM
I think the problem here is the fact that it doesn't seem that you are updating the GUIText's text. =0
I would go about that like this:
var GUITextMessages = GUIText;
function second_textGUI()
{
GUITextMessages.text = "(Sshhh!!)".ToString();
yield WaitForSeconds(3);
GUITextMessages.text = "(Hmm.. who is he?)".ToString();
yield WaitForSeconds(2);
GUITextMessages.text = "(He might be the murderer!!!)".ToString();
yield WaitForSeconds(3);
GUITextMessages.text = "".ToString();
second_en = false;
}
It may not work because I don't usually work with GUI stuffs, so be prepared for errors! XD
Your answer
Follow this Question
Related Questions
Play AudioClips from a List by using yield WaitForSeconds (C#) 1 Answer
Problem with yield 0 Answers
i need to delay an action but my code isnt working 1 Answer
yield return new WaitForSeconds not working 1 Answer
Buffering button inputs 1 Answer