OnTriggerEnter if else

Hello guys, having a little issue with a simple script.

I have it set so once a gameObject with the Tag “enemy” enters a Trigger it changes its tag to another tag and triggers a bool that triggers an if statement. The bool is then changed back once the gameObject has exit the trigger.

But if the gameObject is destroyed while inside of the trigger area then the bool can’t change back so I added an else but it stops the whole script from working? As the bool does not trigger even if the gameObject enters.

void OnTriggerStay(Collider col) {
	if (col.gameObject.tag == "enemy") {
		aggro = true;
	} else {
		aggro = false;
	}
}

void OnTriggerExit(Collider colExit) {
	if (colExit.gameObject.tag == "enemy") {
		aggro = false;
	}
}

So how would I change the bool back to false once and if the enemy gameObject is destroyed while in the trigger area?

Hope this makes sense!

Normally this is the kind of question I would answer-and-reject, but I’m pretty curious to hear any feedback from programming vets who might be able to teach me something new about handling these situations.

Seems like this might be your first time experiencing the shortcomings of event-based logic. It’s a rather complicated discussion, honestly: planning for every contingency while also writing clean and inexpensive logic.

If you’ve got some logic that needs to take place when some event (like a collision response or input event) triggers, and the object that responds to this event needs to also respond to another subsequent event (button released, exit trigger, etc), there are many possible activities that can interrupt this from happening.

Say the user presses a key, this fires a ‘start’ event, then the game or script loses focus before the key is released to fire the ‘end’ event. Or the object you’re manipulating gets destroyed or changed in some way that invalidates the subsequent event you’re waiting for.

These issues have driven me to stop relying on events in this fashion. Rather than plan for every possible contingency, I choose a different - more expensive but more reliable - style of handling such things.

Any time I need a “start” and “end” event to change something about one object, and there exists the potential that the end event will fail, I do not use an “end” event at all, or, I treat that end event as one of several possible outcomes.

Often this involves using OnTriggerStay instead of Enter/Exit. Often it involves using OnButton instead of Down/Up. In other words, it involves checking the status said event would normally check for me every frame. This organizational method gives you the opportunity to check for other conditions which might invalidate what would normally be your “end” event.

I wouldn’t personally use @AlwaysSunny’s suggestion of OnTriggerStay() - I’d continue to use Enter/Exit but also maintain a list of what’s currently in the trigger area - if you want you can then check that list every frame if you want in Update (which would provide similar functionality to OnTriggerStay), or check it less frequently via a coroutine, or at any specific time you want:

In your case, if the list is ever empty (either because all gameobjects left the trigger or because they were destroyed), you set aggro to false.