Trouble getting a function to wait

Hi there,
I have run into this prob a few times so I thought I’d see if someone here can clear it up for me,
Is it possible to use while in a function?
I had a point in my script where I wanted the function to wait until the player’s vehicle had stopped before it continued.
Something like this

	while (vehicleScript.vehicleSpeed > 1){
		print ("Speed"+vehicleScript.vehicleSpeed);	
	}

This made unity crash and, I have a feeling functions can’t do this due to the way they run, is that right? If so is there another way to get a function to pause like this?

Thanks
Pete

You need to use yield and coroutines.

–Eric

ahh, cool. Thanks Eric, for some reason I was thinking functions were coroutines.
This is great! I’ll look into it now.

Functions are automatically coroutines if you put a yield in them somewhere. This makes them of type IEnumerator, which you can add explicitly if you feel like it; i.e.:

function MyCoroutine () : IEnumerator {
  // blah blah blah
}

–Eric