Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by xenonsin · Feb 21, 2014 at 07:56 AM · c#coroutinebooleancooldown

Generic Cooldown IEnumerator?

So I'm trying to make a generic function for all my cooldowns.

I basically have:

 public bool check;
 public float coolDown = 0.5f;
 
 void start()
 {
 check = false;
 }
 
 void update()
 {
     if (!check)
     {
         check = true;
         StartCouroutine(CoolDown(check, coolDown));
     }
 }
 
 IEnumerator CoolDown(bool action, float coolDown)
 {
     yield return new WaitForSeconds(coolDown);
     action = false;
               
 }

But the action = false does not work. If I used check = false during the CoolDown it will work. Is there a way for it to change the value of check without calling check directly?

Comment
Add comment · Show 1
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 CodeElemental · Feb 21, 2014 at 08:39 AM 0
Share

I've heard people were having problems if the coroutine method that takes parameters

     CoolDown(bool action, float coolDown)


is not public. Can you try to make it public and try again ?

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by whydoidoit · Feb 21, 2014 at 08:44 AM

So unfortunately what you want to do is pass a reference to the CoolDown function, rather than the bool directly - however this is not supported with these "special" classes in .NET.

You have a couple of choices:

  1. Pass a class containing the bool and check that:

      public class CoolDownGuard {
               public bool action;
        }
    
         IEnumerator CoolDown(CoolDownGuard guard, float coolDown) {
               yield return new WaitForSeconds(coolDown);
               guard.action = false;
         }
    
    
    

2) Use a generic "run after a period of time" function:

   public IEnumerator DoAfter(float delay, System.Action operation) {
        yield return new WaitForSeconds(delay);
        operation();
   }


     StartCoroutine(DoAfter(coolDown, ()=>check = false));
  
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 xenonsin · Feb 21, 2014 at 09:06 AM 0
Share

The second one worked like a charm, thanks! I'm also wondering if using coroutines for these kinds of operations is best practice.

Before using coroutines I implemented cooldowns by using:

 public float timeStamp = 0.0f;
 public float coolDown = 3.0f;
 
 ...
 
 if(Input.Get$$anonymous$$eyDown("w"))
 {
     if (Time.time > timeStamp)
     {
         timeStamp = Time.time + coolDown;
         //do stuff
     }
 }

Is there a difference with going with the coroutine method or this time stamp method?

avatar image whydoidoit · Feb 21, 2014 at 09:07 AM 1
Share

I use the Coroutine method all the time - it's much much cleaner to read and no big overhead.

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

20 People are following this question.

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

Related Questions

Toggling bools automatically using coroutines 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

make events occur only at specific times 1 Answer

C# - Coroutines for Abilities 1 Answer


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