I need to use yield WaitForSeconds(3); in the function update but can’t how else could you get the same effect?
var cubeSpeed : float = 0.01;
var cubeMoving : boolean = true;
function Update ()
{
if(cubeMoving)
{
transform.Translate(0,cubeSpeed,0);
}
if(transform.position.y >= 10)
{
cubeMoving = false;
gameObject.renderer.material.color = Color.red;
//need to use "yield WaitForSeconds(3);" here
gameObject.renderer.material.color = Color.green;
}
}
var cubeSpeed : float = 0.01;
var cubeMoving : boolean = true;
private var alreadyWaiting : boolean =false;
function Update ()
{
if(cubeMoving)
{
transform.Translate(0,cubeSpeed,0);
}
if(transform.position.y >= 10 && !alreadyWaiting )
{
cubeMoving = false;
Wait(3);
}
}
function Wait(time : float)
{
alreadyWaiting = true;
Debug.Log("time started");
gameObject.renderer.material.color = Color.red;
yield WaitForSeconds(time);
gameObject.renderer.material.color = Color.green;
Debug.Log("time ended");
alreadyWaiting = false;
}
moghes
3
I added on Kral’s answer, since his was incomplete.
You can call the function and lock it (prevent to call) untill the given time passes.
var canCallFunction: boolean = true;
function Wait(time : float)
{
canCallFunction= false;
Debug.Log("time started");
yield new WaitForSeconds(time);
Debug.Log("time ended");
canCallFunction= true;
}
function Update()
{
if(canCallFunction)
Wait(3);
}
Hope this helps