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.

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().

Thank you very much, I solved it :slight_smile: