- Home /
Update cannot be coroutine?
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;
}
Hi I want my Players to not be able to move until a certain amount of time but I don't know how to do that without saying WaitForSeconds
Answer by Tourist · Oct 07, 2014 at 03:58 PM
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?
The update is how my paddles move and when the ball resets I want the players to not be able to move for a certain amount of time
Answer by Anxo · Oct 07, 2014 at 04:21 PM
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. )