I want 3 gameObjects to take damage every 1 second while staying in a trigger: What I'm getting is each cube taking damage one at a time. Help?

void OnTriggerStay(Collider other){
lastDamage += Time.deltaTime;

		if (lastDamage >= 2f){
			if (other.collider.gameObject.GetComponent<Actor>() /*&& Time.time >= lastDamage*/){
				other.collider.gameObject.GetComponent<Actor>().TakeDamage(damage);
				lastDamage = 0;
			}
		}

	}

This is the code I’m using. I have 3 cubes in this trigger with ‘Actor’ scripts attached, they all have trigger colliders, and kinematic rigidbodys. They are all taking damage, But one at a time. The first one will take damage until it runs out of HP and destroy’s it self, then the next cube will take Damage until it destroys itself, ext. What I want is for them all to take damage, then one second later take more damage, they should all run out of hp and destroy themselves at the same time.
What I think is happening is the first one is taking damage and the ‘lastDamage’ float is re-setting making the IF statement fail for the next cube. I have tried putting the timer into the Update statement but that makes it act weird(One cube randomly dies first, followed by the next two at the same time.) I’m not sure what else to do, so if anyone could help I’d be grateful.

This indeed has to do with lastDamage.

First problem: OnTriggerStay can be called MULTIPLE times each frame (once for each collider you hit), it isn’t actually smart to increase this timer here, sinse when you are hitting 2 colliders, the damage will be inflicted twice as fast.

Second problem: If OnTriggerStay is called twice in one frame, for 2 different colliders. And lastDamage >= 2. For the first collider it will do damage and reset. The second collider won’t take damage because lastDamage is 0 again.

Moving this timer to FixedUpdate should work. (To FixedUpdate because of the execution order) I didn’t test this, so there might be stupid errors, but this is what I think should work:

void FixedUpdate()
{
    if (lastDamage >= 2)
    {
        lastDamage = 0;
    }
    lastDamage += Time.fixedDeltaTime;
}

void OnTriggerStay(Collider other)
{
    Actor actor = other.GetComponent<Actor>();
    if (actor != null && lastDamage >= 2)
    {
        actor.TakeDamage(damage);
    }
}

Another solution could be to move lastDamage to the Actor script, and handle it like this…

void OnTriggerStay(Collider other)
{
    Actor actor = other.GetComponent<Actor>();
    if (actor != null)
    {
        actor.lastDamage += Time.deltaTime;
        if (actor.lastDamage >= 2)
        {
            actor.TakeDamage(damage);
            actor.lastDamage = 0;
        }
    }
}

On a sidenote, it might be smart to make ‘2’ a variable instead of a magic number, especially if you choose the first of the 2 solutions :wink: