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
0
Question by ronronmx · Jan 30, 2011 at 05:54 AM · coroutineyield

Yield everytime boolean is True (C#)

Man yield and coroutines are giving me a headache...basically I have a boostCharge, and while I'm boosting, I'm emptying the boostCharge on every frame. I also have a maxBoost which defines the maximum boost amount for the boostCharge. What I'm trying to do is yield for 5 seconds every time boostCharge is less then maxBoost and I'm not boosting anymore.

Here's my current code below:

void Update () {

     // Check if boost is being used...
     if (GUI_Controls.boostOn && boostCharge > 0) {
         boostCharge--;
     }

     if (boostCharge <= 0) {
         bikeEngine.boostIsOn = false;
     } else {
         bikeEngine.boostIsOn = true;
     }

         // Check if boostCharge is less then maxBoost and if boost is being used...
     if (boostCharge < maxBoost && !GUI_Controls.boostOn) {
         StartCoroutine (waitTime (5.0F));
     }

     Debug.Log(boostCharge);

 }


 public IEnumerator waitTime (float seconds) {
     yield return new WaitForSeconds (seconds);
     while (boostCharge < maxBoost) {
         boostCharge++;
         break;
     }
 }

It's working the way I want 50% of the time. Some time the charge starts to refill right away (it doesn't yield) and even when I'm actually boosting, it still tries to refill the boostCharge. Some other times, it works like I intended it to work.

I'm new to coroutines and yield, so this is confusing me. I feel like I'm close but I can't figure out what I'm doing wrong.

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

1 Reply

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

Answer by Skjalg · Jan 30, 2011 at 11:16 AM

You gotta remember that for every frame that boostCharge is less than max boost and you are not holding in your boost button you will start a new coroutine with that code.

So lets say you play this with a 100fps, and as soon as you stop boosting, the game will make 100 new coroutines every second. That makes 500 coroutines before you see the effect of the first one.

My guess is that your logic here is just wrong from the start :) If I were you, I'd just store the time in a local variable for when the last time you let go of the boost button was, and if the time is that local time paramater + 5 seconds, then you can start charging up again...

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 ronronmx · Jan 30, 2011 at 06:27 PM 0
Share

Thanks a lot for your help, I will try what you suggest as it seems like a better (and easier) way to achieve what I want. I'm trying to wrap my brain around coroutines but I'm having a hard time figuring them out! Now let's say I had to use a coroutine to achieve what I need, how would I change my code to make it work correctly?

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

No one has followed this question yet.

Related Questions

Pause the coroutine inside a loop 1 Answer

Last item not being moved (yield problem) 0 Answers

Coroutoutine doesnt work properly. 3 Answers

Im a bit confused on a simple script 1 Answer

How To cycle coroutine in C# say every 1 sec? 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