which is the best way to reset an variable when OnTriggerExit2D is not executed?

I’m using bullet that is destroyed when trigger enter event happens.
i’m reseting my variable isHurt like this.
Is there a better way to reset this variable?

int count=2;
 //-------------------------------------------------
    // Hit detector
    //-------------------------------------------------
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (!state.isHurt && hiters.CheckHiters(collision))
        {
            state.isHurt = true;
            count=0;
        }
    }
    //-------------------------------------------------
    // Reset hit
    //-------------------------------------------------
    private void OnTriggerExit2D(Collider2D collision)
    {
        if (state.isHurt && hiters.CheckHiters(collision))
        {
            state.isHurt = false;  
        }
    }
 
    //-------------------------------------------------
    // Reset hit
    //-------------------------------------------------
    private void Update()
    {
        if (state.isHurt && count == 1)
        {
            state.isHurt = false;
        }
        if (int.MaxValue == count)
        {
            count=2;
        }
        count++;
    }

Thank you so much!!

Okay, so I’m guessing this is how you want things to work:
Bullet hits player, player is ‘hurt’ for a period of time, bullet is destroyed (so never exits).
Assuming that is the case, what you’d want to do is put in a timer of some kind, and that should use FixedUpdate instead of Update. Update fires every frame, and since that can radically vary, you don’t generally want to put gameplay logic there. Update is meant more strictly for your graphical logic. You can use fixeddeltatime to figure out home much time has passed each call to fixedupdate and just keep reducing a ‘hurt time’ variable by such until it is at or below zero and then you are no longer hurt and can reset such.

1 Like

i think triggerexit doest get called if object is destroyed inside it,
maybe could move the bullet really far, if that would trigger it.

for timer, could start coroutine

1 Like

Yes!! that is. good idea!! i going to try it. Thank you so much for you help an for you time!! :slight_smile:

Thank you for your comment. I’m going to try first with the Derekloffin idea. i have read that the coroutines is better avoid it and only use it if absolutely necessary. But it can be another option too.
Thank you so much for your help.