x


[Closed] 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);    
 }
more ▼

asked Jul 21, 2011 at 01:15 PM

awkwardtodd gravatar image

awkwardtodd
1 13 10 10

(comments are locked)
10|3000 characters needed characters left

The question has been closed Apr 27, 2013 at 12:10 PM by Fattie for the following reason:

Duplicate Question


6 answers: sort voted first

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).

more ▼

answered Jul 21, 2011 at 03:21 PM

Chris D gravatar image

Chris D
2.7k 14 15 39

disclaimer: I still don't have much experience with coroutines, but I think it should work :D

Jul 21, 2011 at 03:23 PM Chris D
(comments are locked)
10|3000 characters needed characters left

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++;

  }
more ▼

answered Jul 21, 2011 at 03:29 PM

BerggreenDK gravatar image

BerggreenDK
2.7k 154 125 148

in his case above, it might as well be

 renderer.enabled = !renderer.enabled
Jul 21, 2011 at 03:33 PM Chris D

yeah, I didnt notice the blink was on and off only.

Jul 22, 2011 at 09:49 AM BerggreenDK
(comments are locked)
10|3000 characters needed characters left

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;
 }  
more ▼

answered Jun 11, 2012 at 10:16 PM

Skorcho_legacy gravatar image

Skorcho_legacy
0

(comments are locked)
10|3000 characters needed characters left

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);
         }
     }
 }
more ▼

answered Jun 12, 2012 at 01:38 AM

Berenger gravatar image

Berenger
12.9k 40 45 98

(comments are locked)
10|3000 characters needed characters left

I've never been a fan of StartCoroutine and yield. I much prefer using Invoke and InvokeRepeating.

You can have a blink count variable and a conditional to check how many times it has blinked, and have a TurnOff() function that will Invoke the TurnOn() function in 0.3 seconds or 3 seconds depending on whether it's done blinking, and CancelInvoke() when it's done.

more ▼

answered Aug 02, 2012 at 10:15 PM

Kiloblargh gravatar image

Kiloblargh
5k 194 173 198

(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x831
x542
x526
x285
x37

asked: Jul 21, 2011 at 01:15 PM

Seen: 15947 times

Last Updated: Apr 27, 2013 at 11:03 AM