- Home /
Duplicate Question
A delay/yield/wait function for function Update?
I am having some trouble getting a delay to work in the Update functon. I'm trying to call a blinking function on a loop so it waits a few seconds before blinking, then blinks, then repeats.
I tired to us a while loop but it just creates infinite recursion. After that I tried to use a blinkwaiting fucntion to hold the yield waitForSeconds function but I'm still having infinite recursion.
Any help would be wonderful thank you.
private var isBlinking : boolean = false;
// Start function
function Start()
{
yield WaitForSeconds(1); // waits one second before blinking
//StartCoroutine(Blink(3.0)); // activates the Blink function
}
function Update ()
{
while(isBlinking == false)
{
delayTillBlink();
blinkDelay();
}
}
function delayTillBlink ()
{
yield WaitForSeconds(2.0);
}
function blinkDelay ()
{
renderer.enabled = false;
yield WaitForSeconds(0.3);
renderer.enabled = true;
yield WaitForSeconds(0.3);
renderer.enabled = false;
yield WaitForSeconds(0.3);
renderer.enabled = true;
yield WaitForSeconds(0.3);
}
Answer by laurentlavigne · Apr 27, 2013 at 11:03 AM
bool blinkingState = Mathf.FloorToInt( Time.time*blinkingSpeed) % 2 >0;
Answer by Chris D · Jul 21, 2011 at 03:21 PM
isBlinking is always false so you're going to be running through a new blinkDelay
every update.
I've seen a lot of coroutines run from the Start function so you might want to consider trying that. I.E.:
function Start(){
while(true)
yield StartCoroutine("blinkDelay");
}
Check out the docs for more examples (also search the site, there're some around).
disclaimer: I still don't have much experience with coroutines, but I think it should work :D
Answer by BerggreenDK · Jul 21, 2011 at 03:29 PM
have you considered to use simple timing/counters with states?
You could keep a floating counter like:
float delayCount = 5.0f;
int blinkState = 1;
then inside your Update() you manually decrease with the Time.deltaTime value.
delayCount -= Time.deltaTime;
if (delayCount<0)
{
delayCount = 5.0f;
switch(blinkState)
{
case 1:
// do what ever state 1 does.
break;
case 2:
// do what ever state 2 does.
break;
default:
blinkState = 0; // init,if we get outside max
break;
}
blinkState++;
}
in his case above, it might as well be
renderer.enabled = !renderer.enabled
Answer by Skorcho_legacy · Jun 11, 2012 at 10:16 PM
I find that yielding inside the while loop tends to sort out infinite loop crash, but might be wrong :(
while(isBlinking == false)
{
delayTillBlink();
blinkDelay();
yield;
}
Answer by Berenger · Jun 12, 2012 at 01:38 AM
Not sure you still need that information, but anyway :
// You must call that function when the blink cycle starts. Probably in Awake/Start.
public IEnumerator BlinkLoop()
{
// You can stop the coroutine (not right away, the blinking will have to finish)
// by setting this var as false.
isBlinking = true;
while( isBlinking )
{
yield return new WaitForSeconds( waitBeforeBlink );
for(int i = 0; i < blinkCount; i++ )
{
renderer.enabled = false;
yield return new WaitForSeconds( blinkFreq);
renderer.enabled = true;
yield return new WaitForSeconds( blinkFreq);
}
}
}