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 ProTheJoker · Oct 21, 2013 at 03:28 PM · loopwaitforsecondsnotwhileworking

WaitForSeconds not working in while

Here again another question, sorry to bother you.

I was trying to do a function to increase mp every seconds while maximum mps are greater than the current mps Look at the function:

 function mpcharge() {
     while (maxmp > curmp) {
         yield WaitForSeconds(1);
         curmp++;
     return;
     }    
 }

I'm calling it from update if (am.A1Active == false) mpcharge(); else mpdecrease();

am.A1Active is a mp consuming skill of the character. (If it's not activated then recharge one mp per second).

Thanks.

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 mattmanj17 · Oct 21, 2013 at 03:54 PM 0
Share

there might be something helpfull on this page. http://answers.unity3d.com/questions/172055/help-with-yield-wait-for-seconds-.html

or this page http://answers.unity3d.com/questions/27996/better-way-to-delay-a-function-for-a-few-seconds-j.html

I usually use c#, and coroutines are different in javascript, but the first thing i would suggest to try is moving the code inside the while into another function, then calling that function from within the while (this is the strategy used in the first link).

also, don't call the function from update, as the coroutine will be called every frame (the second link talks about this).

2 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by robertbu · Oct 21, 2013 at 03:53 PM

Calling it from Update() is your problem. You want to call it only once...perhaps in Start(). Every update call is spawning a new coroutine. If your app is running at 60 fps, you will be adding 60 coroutines per second to your game, all executing at the same time.

The easiest fix would be to move the logic into Update(). Assuming curmp is a float, you could just do:

 if (!am.A1Active)
     curmp += Time.deltaTime;

This will increment curmp a fraction of a second in each Update().

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 ProTheJoker · Oct 21, 2013 at 04:02 PM 0
Share

This worked, but only if I put curmp as a float. I'd like to leave it as an integer. And what if I want to wait more than a second? ( Or less? )

avatar image robertbu · Oct 21, 2013 at 04:18 PM 0
Share

With the float solution, you would multiply by the amount you want to increase per second.

 curmp += Time.deltaTime * rate;

Setting rate to 0.5 for example would increase by 1.0 every two seconds.

If you want to leave it as an integer and only check it every second, then you have several choices. A coroutine is one of them, but you cannot do it the way you did it. InvokeRepeating() is another. Or you can use what I call a 'timestamp'. In Update() your logic would be:

 if (!am.A1Active && Time.time >= timestamp) {
    curmp++;
    timestamp = Time.time + futureSeconds;
 }

To check each second, set futureSeconds to 1.0;

avatar image thef1chesser · Oct 21, 2013 at 04:18 PM 0
Share

$$anonymous$$aking it an integer would not work. Because an integer would cut off after the full digit which would stay a 0. It would get + 0.016667 seconds with 60 fps.

If you want to wait longer then you increase the digit you use for your if statement. (and decrease it for a shorter time) For 1 second use >= 1

avatar image
0

Answer by ProTheJoker · Oct 21, 2013 at 07:32 PM

Thank you very much, I solved it :)

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 YoungDeveloper · Oct 21, 2013 at 07:34 PM 0
Share

None of the answers didn't help? Otherwise you should tick question as answered. And post how did you solve it, for others..

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

18 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

Related Questions

Yield Not Working - While Loop 3 Answers

OnTriggerEnter not working 0 Answers

While loop crashing 2 Answers

Problem with while statement. 1 Answer

Timed while loop? 0 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