Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Apr 27, 2013 at 12:10 PM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by awkwardtodd · Jul 21, 2011 at 01:15 PM · updateloopyieldwaitforsecondsblinking

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);    
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

6 Replies

  • Sort: 
avatar image
1

Answer by laurentlavigne · Apr 27, 2013 at 11:03 AM

bool blinkingState = Mathf.FloorToInt( Time.time*blinkingSpeed) % 2 >0;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chris D · Jul 21, 2011 at 03:23 PM 0
Share

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

avatar image
0

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

  }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chris D · Jul 21, 2011 at 03:33 PM 0
Share

in his case above, it might as well be

 renderer.enabled = !renderer.enabled
avatar image BerggreenDK · Jul 22, 2011 at 09:49 AM 0
Share

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

avatar image
0

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;
 }  
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
  • 1
  • 2
  • ›

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Checking variable each frame to increase another variable every few seconds 1 Answer

Load Level when dead for 1 second 2 Answers

Simple rotation script. (loop) 3 Answers

Definition of Script.function() depends on Script.function()... 3 Answers

Yield Not Working - While Loop 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges