Yield WaitForSeconds(300) LateUpdate() cannot be a coroutine

I would like to let the character reset to the start position and be waiting for 3 seconds after it die.

function LateUpdate () 
{
	if(dead)
	{
		transform.position = Vector3(0,0,0);
		gameObject.Find("Camera Controls").transform.position = Vector3(0,0,0);
		dead = false;
		yield WaitForSeconds( 300 );
	}	
}

However, it throws an error:
Script error: LateUpdate() can not be a coroutine.

How should I fix it?

Update and LateUpdate cannot be coroutines, but they may start them:

bool isRespawning = false;

function LateUpdate ()
{
   if(dead  !isRespawning)
   {
      isRespawning = true;
      DeathRoutine();
   }   
} 

function DeathRoutine ()
{
      transform.position = Vector3(0,0,0);
      gameObject.Find("Camera Controls").transform.position = Vector3(0,0,0);
      
      yield WaitForSeconds( 3 );

      dead = false;
      isRespawning = false;
}

Yeah you can’t use yield in Update or LateUpdate or OnGUI or any of those, you need to make your own function for it. I’ve not put this in Unity to be 100% sure it works, but it should.

function LateUpdate () 
{ 
	if(dead) 
	{ 
		resetToStart();
	}    
}


function resetToStart()
{
	dead = false; 
	transform.position = Vector3(0,0,0); 
	gameObject.Find("Camera Controls").transform.position = Vector3(0,0,0); 
    yield new WaitForSeconds(3.0); 
}

Better than the above would be to call resetToStart() werever dead gets set to true. HTH

EDIT: LOL, looks like BoredKoi was responding at the same time as I. For clarification - when I wrote “Better than the above would be to call resetToStart() werever dead gets set to true” I meant better than having LateUpdate wait for dead to be true. I did not mean better than BoredKoi’s post above mine. :slight_smile:

LOL’n too – yeah I’ve done that before (posting right when someone else is). No worries, plus you’re right down the wonderful_road_that_is_I95 from me :slight_smile: