- Home /
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.
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).
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().
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? )
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;
$$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
Answer by ProTheJoker · Oct 21, 2013 at 07:32 PM
Thank you very much, I solved it :)
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
Follow this Question
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