Understanding yield

I need some help understanding how to use yield properly. When I click on an object, I need it to wait 5 seconds before moving to another location. My understanding was that yield goes in a separate function if intended to be use in the update, but when I run it this way, nothing appears to happen. Have I misunderstood yield?

The way I have this set up, it doesn’t seem to be doing anything at all. Any ideas what I’m doing wrong?

function Update ()
	if (numberOfClicks <= 0)
		PauseBeforeMove ();
		var position = Vector3 (Random.Range(-6,6),Random.Range(-4,4),0);
		transform.position = position;
		numberOfClicks = storeClicks;	

function PauseBeforeMove ()
	yield WaitForSeconds(5);

You can never cause Update to be delayed in any way; it always runs once per frame, and running another function won’t change that. You can put the rest of the code in the PauseBeforeMove function, or you could remove Update altogether (see here for more info about that).

A yield function cannot be called in an update.

I know you don’t have a yield function in your update function, but you do in your PauseBeforeMove, which is being called by the update.

So really, your PauseBeforeMove function is also being called everyframe - you want to use a boolean:


var pauseOn : boolean = true;

--- in your update ---
if(pauseOn == true){
   pauseOn = false;

This ensures that your PauseBeforeMove will only be read once - thus making your yield work.