while loop not looping

I’m sure I’ve done this same thing many times before, but this time it doesn’t appear to be working. The code will print Time only once and then will quit the function on the yield.

function Boost ()
	var destTime = Time.time + 20;
	while ( Time.time < destTime )
		Debug.Log ( Time.time );
	Debug.Log( "Loop complete" );    // <~~ This is never reached

Coroutines are run on the GameObject that had the script that called StartCoroutine. Note that in UnityScript the StartCoroutine is implied, if the function has a yield in it.

So, let’s say you have a player and an item. If you have the Player call Boost() on the item, the Boost() coroutine will be run on the Player’s GameObject. If you have the Item call Boost(), the coroutine will be run on the Item’s GameObject.

You can see this behavior with the following scripts:

//Attached to a GameObject named "1"
using UnityEngine;
using System.Collections;

public class MyBaseClass : MonoBehaviour {
	public void StartCor(){
		StartCoroutine(Cor ());
	public IEnumerator Cor(){
			yield return null;

//Attached to another GameObject
using UnityEngine;

public class MyExtendedClass : MonoBehaviour {
	void Start(){

Depending on who called StartCoroutine, disabling that GameObject will also stop the coroutine.

Your problem will most likely be solved by calling Boost() from somewhere else than the Item script.

The yield is probably the problem.

But that’s not the right way to use a while loop, anyway. Either use InvokeRepeating or set a boolean (isBoosted) and do something in Update() if the boolean is true. Define your destTime variable in the main script, not in the function that sets it.