coroutine not working in update?

I am getting this error

Script error (apc collision): LateUpdate() can not be a coroutine.

After I am detected by the enemy I want to wait 5 seconds then be sent to the death screen menu

Without the yield it works, but it goes straight into the next scene without waiting

So I add a yield waitforseconds but it doesn’t work

I have another script like which goes into a scene for 6 secodns then automatically goes back to another scene so I tried using the same logic but it;s not working here

This is the improtant part of ym script

function LateUpdate()
{
if (spawn) //if player dies reset back all the player info 
{
//gameObject.Find("fps").transform.position = Vector3(150,80,160);
b = true;
health = 5.0;
spawn = false;
hidden = false;



}



if (b == true)
{
Debug.Log("wait 5 seconds");
 yield WaitForSeconds (5);
 Application.LoadLevel("DeathScene");
}

}

Where am i supposed to put the yieldwaitforseconds? What is a coroutine, how does this all work?

Any kind of Update function can’t be a coroutine, since it runs every frame without exception and can’t be delayed. Don’t use LateUpdate…all you’re apparently doing with it is constantly polling the spawn variable, which isn’t a good idea. Just make a function that’s called once when you need to spawn, then it can be a coroutine.

–Eric

You should always start a coroutine.

Hey thanks guys so my problem was I was trying to do it in the lateupdate function which I just found out to be useless.

I just made it really simple and it works

Please let me know if the code looks wrong or if I am doing it wrong

Also Dantus thanks but it says javascript does it automatically?

Also it works now, is it working because I am lucky, or am I still doing it wrong?

Where in my script did I actually start a courotine?

Here is the script that makes it wait 3 seconds then load into next scene

if (player.gameObject.CompareTag ("Player"))

{

  var playerScript : run = player.GetComponent(run);

  if (playerScript.hidden == true  activation == true)

  {
    Debug.Log("apc has not detected you");
  }
  
  else
  {
  health = health - 1;
  Debug.Log("apc has detected you " + health);
  yield WaitForSeconds (3);
  Application.LoadLevel("DeathScene");
  }

}
}

These 2 liens

yield WaitForSeconds (3);
Application.LoadLevel(“DeathScene”);

Now if I put these two lines in a separate function it doesn’t work ):

Thanks again