It says my Update() can not be a coroutine what should I Do to fix it
#pragma strict
var moveUp : KeyCode;
var moveDown : KeyCode;
var speed : float = 15;
function ResetPlayers ()
{
transform.position.y = 0;
}
function Update ()
{
if (“ResetPlayers”) yield WaitForSeconds (4);
}
if (Input.GetKey(moveUp))
{
rigidbody2D.velocity.y = speed;
}
if (Input.GetKey(moveDown))
{
rigidbody2D.velocity.y = speed *-1;
}
else
{
rigidbody2D.velocity.y = 0;
}
In the function OnEnable, start your update coroutine and stop it in OnDisable.
Your update coroutine should never end and be like :
IEnumerator MyUpdate()
{
while(true)
{
// your code
yield return null;
}
}
This code sample is C#. Use the javascript equivalent if you need it.
But most important, what is the point of having a coroutine to manage an update?
Anxo
October 7, 2014, 4:21pm
3
Tourist was showing you in C# that you could just create a function that does your yielding for you. So you could have a external function that would reset the players
playersAreReady : bool = true;
function Update(){
if(playersAreReady){
//your code here.
}
}
function ResetPlayers(){
playersAreReady = false;
yield WaitForSeconds(4);
playersAreReady = true;
}
Then from somwhere else you can just reset players like this
ResetPlayers();
UC (Warning: Uncompiled code. Check for Errors. )