I'm obviously not a great programmer, and I'm having a problem at the moment that I think stems from a fundamental misunderstanding of coroutines and yield.
Basically, I have a function used to display GUI texts according to a numbered variable when the player activates a terminal etc. I want the GUI message to display until the player hits a key, and then load the next message ( very similar to this, but I can't figure it out)
So the two scripts are as follows (names simplified):
//this function enables the GUI script
messagenumber = textnumber;
GetComponent(GUIDisplay).enabled = true;
//this function turns the GUI off on keypress,
//so that it can be reloaded with the next value
GetComponent(GUIDisplay).enabled = false;
Now, as I (mis)understand it, yielding the commands in Script 1 means that Script 1 will wait until each function is complete before moving to the next one. My problem is that I can't get Script 2 (especially the MoveOn function) to work. I can get an infinite loop(crash), or simply cycle through all three commands in Script 1 instantly. I have read in other posts that while loops need a yield to prevent infinite looping, but it seems to me that putting in a yield simply gives control back to Display(), defeating the purpose of the loop (i.e. waiting for a keypress before ending)
BTW, it works fine using yield WaitforSeconds, but I want the time to be set by the user (for slow readers!)
Any ideas what I'm missing here? Any commments much appreciated, and feel free to point out how little I know!
Answer by DaveA
Mar 01, 2012 at 12:49 AM
Yeah, no. You should look for Input in an Update function, set whatever variables would indicate progress there, and use those in the OnGUI function to display which ever UI bits you want at that stage. OnGUI would 'wait' for the user to type something in, for example.
I suppose you might use Yield and such to do such things, but it would not be 'best practice'
Thanks for your reply. I'm trying to do this using "global" coroutines, so that I can bring up the display I need with minimal trouble later (I will need to do this a lot). I tried setting some booleans in Update, and using these to signal the while loop to end but I still couldn't get it to work. Purely theoretically I can't see how while loops can ever work with yield, though it seems others have made them work.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a user guide.
If you are a new user, check out our FAQ for more information.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
For troubleshooting common problems with Unity 5.x Editor (including Win 10).
Answers and Comments
5 People are following this question.
How to properly use Yield for a coroutine?
Best way to wait for event inside Coroutine?
Yield until color has been chosen, return color (or Color index)
Alternative for semaphores in Unity?
Wierd issue with Coroutines?