- Home /
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!
Answer by AlwaysSunny · Jun 08, 2015 at 07:06 PM
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.
Answer by tanoshimi · Jun 08, 2015 at 07:31 PM
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:
https://alastaira.wordpress.com/2013/12/12/keeping-track-of-targets-in-a-trigger/
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.
Oh yeah, I didn't elaborate that far, did I? Good on ya! I've used similar strategies in the past. Guess I asked wanting to make absolutely sure I wasn't missing some kind of magic that would make these problems disappear.
Your answer
Follow this Question
Related Questions
Keycode else 1 Answer
I am getting error cs1525 for '}' on line 11,17. How come? 1 Answer
Void OnTriggerEnter not working 1 Answer
BCE0044 Error in the Start function 1 Answer
problem with instance 1 Answer