Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Bug Coroutine was working yesterday but not working today

Discussion in 'Scripting' started by imVarcolac, Jul 16, 2023.

  1. imVarcolac

    imVarcolac

    Joined:
    Dec 7, 2020
    Posts:
    8
    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.
    upload_2023-7-16_10-29-43.png
    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...
     
  2. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    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
     
    Bunny83 and imVarcolac like this.
  3. imVarcolac

    imVarcolac

    Joined:
    Dec 7, 2020
    Posts:
    8
    i logged current health it starts with original value, decreses once then starts to log the same health (159 etc.)
     
  4. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    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
     
    Last edited: Jul 16, 2023
    Bunny83 and imVarcolac like this.
  5. imVarcolac

    imVarcolac

    Joined:
    Dec 7, 2020
    Posts:
    8
    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
     
    Last edited: Jul 16, 2023
  6. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    remove the part of the code that has .raise

    prob that is causing the bug
     
  7. imVarcolac

    imVarcolac

    Joined:
    Dec 7, 2020
    Posts:
    8
    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
     
  8. imVarcolac

    imVarcolac

    Joined:
    Dec 7, 2020
    Posts:
    8
    upload_2023-7-16_11-32-48.png
    event triggers this function.
     
  9. imVarcolac

    imVarcolac

    Joined:
    Dec 7, 2020
    Posts:
    8
    Oh i found out the bug thanks everyone that helped me

    Also since i wrote in the wrong thread should i delete it?
     
  10. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    How did
    this happen then o_O

    Events are generally raised like this
    Code (CSharp):
    1. class Counter//https://learn.microsoft.com/en-us/dotnet/standard/events/
    2. {
    3.     public event EventHandler ThresholdReached;
    4.  
    5.     protected virtual void OnThresholdReached(EventArgs e)
    6.     {
    7.         ThresholdReached?.Invoke(this, e);//you'll want to null check the usual way instead of using the ?. operator in Unity
    8.     }
    9.  
    10.     // provide remaining implementation for the class
    11. }
    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:
    1. Any conversion between object type to/from another will lead to boxing, massive performance hit
    2. don't insantiate a new waitforseconds inside the coroutine everytime it loops, you can cache it
    3. coroutines aren't the fastest option, async methods don't run in the main thread and they generally lead to better performance
    4. don't worry about performance you are just making a 2d platformer
     
    imVarcolac likes this.
  11. venediklee

    venediklee

    Joined:
    Jul 24, 2017
    Posts:
    143
    No, just type the bug and the solution in here and mark this thread as solved
     
  12. imVarcolac

    imVarcolac

    Joined:
    Dec 7, 2020
    Posts:
    8
    upload_2023-7-16_11-51-9.png 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
     
  13. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    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
     
  14. imVarcolac

    imVarcolac

    Joined:
    Dec 7, 2020
    Posts:
    8
    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
     
  15. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    For the future, please use the Scripting forum for scripting questions, not the 2D forum. Use the 2D feature forum when asking about 2D features. I'll move this thread for you.

    Also, please use code-tags when posting code as opposed to taking images of code.

    Thanks.
     
    Bunny83 and imVarcolac like this.