collision deal damage to all objects inside

Hello, i have a little problem with my collider, i need this collider deal damage to all enemies inside but it work only for random one.

void OnTriggerStay2D(Collider2D collision){
        if (collision.gameObject.tag == "Enemy") {
            if(i > shootRate) {
                collision.gameObject.GetComponent<Body>().curHp -= dmg;
                i = 0;
            }
        }
    }

thx for help.

All enemies inside what?

What is “i”?

1 Like

i need if enemies go inside collider than all take dmg over time, and i is… i += Time.deltaTime;

Well there’s your problem, I think. You have only one “i” for all your objects. It happens to go past shootRate at some point, and you apply damage to one object, and then immediately reset i to 0, so for the next object, it’s no longer past shootRate.

Perhaps you should flip this all around and do the damage on the enemy objects, instead of on whatever thing this script is currently on?

yes in past i have dmg control on enemy script but i have more types of enemies and i dont want write it to all enemies i thought it would be better this way.
And this if(i > shootRate) is not important, it only control how often must enemy inside colider get dmg, i only need find all gameobject with Body script and give him dmg.