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 /
avatar image
0
Question by ronronmx · Jul 11, 2011 at 04:05 AM · coroutineienumerator

Turn bool on/off x time every few seconds in coroutine

So I feel really stupid about this problem, I'm sure I'm missing something small.

I'm basically trying to turn a boolean on and off x amount of time at a specific interval. I'm using a coroutine for that, which has 3 passed variable:

blinkCount
blinkSpeed
renderer

What I need to do is:

switch boolean flag for each blinkCount every x seconds. Here's what I have so far:

 using UnityEngine;
 using System.Collections;
 
 public class BlinkObject : MonoBehaviour
 {
 private static bool blinkOn = false;
 
 public static IEnumerator BlinkMultipleObjects(int blinkCount, float blinkSpeed, Renderer objRenderer) {
         
         while( blinkCount > 0 )
         {
             blinkCount--;
             yield return new WaitForSeconds(1f);
             blinkOn = !blinkOn;
     
             // Turn the object's renderer on and off.
             objRenderer.enabled = blinkOn;
             
             yield return null;
         }
     }
 }

The code above keeps turning the blinkOn flag on and off on every frame, instead of every 1 second. I know I'm doing it wrong, I just don't know how to make it work the way I want!

Thanks in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Eric5h5 · Jul 11, 2011 at 06:23 AM

The main problem is that you're using "static"--remove that, since it prevents the coroutine from working. Coroutines shouldn't be static, since they can have any number of instances. Some other things: you can toggle the renderer itself, so you don't need the blinkOn variable (which should be local to the coroutine anyway, not global). You're not using the blinkSpeed variable; presumably that should be used in WaitForSeconds. The "yield return null" at the end isn't necessary since you're using WaitForSeconds anyway. Kind of trivial, but "BlinkMultipleObjects" isn't an accurate name, since only one object is being affected.

 using UnityEngine;
 using System.Collections;
 
 public class BlinkObject : MonoBehaviour
 {
     public IEnumerator ToggleRenderer (int blinkCount, float blinkSpeed, Renderer objRenderer) {
         while( blinkCount-- > 0 )
         {
             yield return new WaitForSeconds(blinkSpeed);
             objRenderer.enabled = !objRenderer.enabled;
         }
     }
 }
Comment
Add comment · Show 3 · 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 ronronmx · Jul 16, 2011 at 05:44 PM 0
Share

Eric5h5, thanks for your help...I knew I was over-complicating things a lot hahaha. One quick question, why does a static variable prevent a Coroutine from running correctly?

avatar image Eric5h5 · Jul 16, 2011 at 05:47 PM 0
Share

Static means one instance, and coroutines can have any number of instances.

avatar image ronronmx · Jul 17, 2011 at 06:14 AM 0
Share

But can you force a coroutine to only have 1 instance, or is that just stupid? For example, I have a few static coroutines, one starts the raceStart timer and will always be only 1 instance no matter what. Is that like bad practice or is it really a big no-no?

Thanks for your time Eric5h5, I really appreciate the help!

avatar image
0

Answer by Chrisg · Jul 11, 2011 at 04:26 AM

Frames happen at different speeds on every computer, based on cpu access and speed. You can't use them as measuring sticks. Instead, you use the time between each frame, and add it together to get seconds/minutes, etc. This is accessed via Time.deltaTime. I use JS, not c# so I'm not sure on the exact implementation, but something like

 float delay = 5; //pause between toggles
 float ticks = 0.0; //time since last toggle
 bool toggle = true; //toggle var
 
 function(delay, ticks, toggle)
 {
    if(ticks >= delay)
    {
       toggle != toggle;
       ticks = 0;
    }
    else
       ticks += Time.deltaTime;
  }


should direct you toward the c# answer(I don't imagine it's far different).

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 Eric5h5 · Jul 11, 2011 at 06:23 AM 0
Share

ronronmx's code isn't using frames (at least that's the intention), it's a coroutine using WaitForSeconds. You don't need to use Time.deltaTime for anything here.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

yield return M() vs yield return StartCoroutine 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Returning an IEnumerator as an int? 1 Answer

Waiting twice inside coroutine (C#) 2 Answers

wait a second before say path is valid? 2 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