So after i finished my imaginary deadline i closed unity and sat back again in the evening only to realise my coroutine suddenly stopped working.

May be important: I crashed my game about 3 times cuz i used coroutine in the wrong place. Also tried the same code with DoScale().Oncomplete didnt work
logs are working…
Are you sure the currentHealth is >= 0 at start? Maybe it hasn’t been serialized correctly or if you have domain reload disabled it may have been messed up because it is a static field???
Anyways, attach a debugger and place breakpoints to places that call TakeDamage and/or inside the TakeDamage method to see what is truly going on
i logged current health it starts with original value, decreses once then starts to log the same health (159 etc.)
Ohh just saw the bug, the coroutine’s logic is in the while loop; it applies the damage continuously until it drops to <0
Not sure why this is a coroutine, are you trying to apply constant damage?
BTW this is a general scripting question, not 2D. You should create threads like this in the scripting thread next time
yes, i didnt want to use while loop in the update function due to performance concerns.
Can you suggest any way of solving this? is it ok to use while loop in the update function.
game is for mobile phones
oh sorry im new to the forum next time ill post accordingly
thanks
btw i dont understand why it causes problems with while. isnt it should do whats in the loop then check again for the condition.
thanks in advance
remove the part of the code that has .raise
prob that is causing the bug
i did and it actually worked in the logs. However i use it to trigger an event causing health.text to update in the game.
i use the same call in some other script and it works fine. Why do you think that happens

event triggers this function.
Oh i found out the bug thanks everyone that helped me
Also since i wrote in the wrong thread should i delete it?
How did
this happen then ![]()
Events are generally raised like this
class Counter//https://learn.microsoft.com/en-us/dotnet/standard/events/
{
public event EventHandler ThresholdReached;
protected virtual void OnThresholdReached(EventArgs e)
{
ThresholdReached?.Invoke(this, e);//you'll want to null check the usual way instead of using the ?. operator in Unity
}
// provide remaining implementation for the class
}
You can use a custom event args struct instead of the empty one, there should be an example in the link
PS2: since you want performance you should know that:
- Any conversion between object type to/from another will lead to boxing, massive performance hit
- don’t insantiate a new waitforseconds inside the coroutine everytime it loops, you can cache it
- coroutines aren’t the fastest option, async methods don’t run in the main thread and they generally lead to better performance
- don’t worry about performance you are just making a 2d platformer
No, just type the bug and the solution in here and mark this thread as solved
i
i created my own event system with references
FOR THE BUG
OnEventRaised i inadvertently called i function which made health text equal to a constant value.
So simply one script tried to change it and other tried to fix it
I dont know why because i dont know what that code does, since u didnt post it
it was just the only thing that was unknown that could be messing with your coroutine
this might seem a bit dumb but i cant seem to find “mark as resloved” button or such.
Thanks again for everybody that tried to help me with their personel time and effort <3