- Home /
OnTriggerExit not triggered when object/trigger inside is destroyed c#
I am trying to do the following: I've got an enemy with a sensor that stops walking when it enters a trigger of a destructible wall. It then starts to attack. When the wall's health is 0 I want the enemy to start walking again. So far so good...on paper.
OnTriggerEnter makes the enemy stop walking, OnTriggerStay makes the enemy attack, OnTriggerExit should make the enemy start walking again.
The issue I am getting though is that the OnTriggerExit of enemy sensor is never triggered. When I "get rid" of the wall (by setting it to a layer that has no collision with the enemy or destroy it) the enemy stills says it is stuck in the wall trigger according to the debug.log. When I animate the wall to move the enemy will start moving again until it hits the wall's new position. Moving it a gazillion units away from its current position, as suggested in other threads, doesn't really seem like a nice sollution, right?
I am getting the feeling as if getting rid of the wall, while the enemy's sensor is static, doesn't properly trigger the OnTriggerExit as I'd expect.
Another similar issue occurs where I have a sentrygun shoot when an enemy enters its trigger. Everything works perfectly but as the enemy is destroyed in the trigger, the sentrygun never gets the OnTriggerExit which results in a sentrygun that keeps on firing as if there is no tomorrow.
Any ideas what I am missing here? I am using C# by the way. This is the script that is attached to the enemy sensor.
void OnTriggerEnter (Collider other)
{
walk = false;
}
void OnTriggerStay (Collider other)
{
if (other.gameObject.CompareTag("LevelDestructible"))
{
other.gameObject.SendMessage("ChangeHealth", damage * Time.deltaTime, SendMessageOptions.DontRequireReceiver);
Debug.Log("collide with destructible");
}
}
void OnTriggerExit (Collider other)
{
walk = true;
}
$$anonymous$$oving the object's transform.position isn't really an elegant solution when you think about it, but the way the physics steps works inside of Unity for your specific scenario does make sense.
If you're relying on OnTriggerExit, it's honestly the best approach. As long as your call ordering is correct, it should fire reliably if done correctly. I've done it before, and it plain works.
Answer by ThePunisher · Nov 21, 2012 at 05:59 PM
Why don't you simply get a target with OnTriggerEnter of whatever type it is you have (like Wall) while the wall's health is still greater than 0 or while you still have a target, continue to attack the target in an update loop. If you lose your target or your target's health reaches a value, then stop attacking and continue movement.
I think you are misusing the OnTriggerStay method with what you are doing. A better example for how to use it would be the following.
You have a particular room filled with a substance that deals damage to your character. If your character enters (OnTriggerEnter) the collider associated with the substance, the player takes an initial amount of damange. Now, you can use OnTriggerStay to make the player take periodic damage while he remains in the substance/collider.
Thanks for the reply! I could use the target method as you described but then I'd have to check every frame if the target is still within a certain distance or area. $$anonymous$$ore important for other moving objects that walk away from the enemy than a static wall, but still. Isn't this what the triggers would be ideal for to use?!
Your last example doesn't seem a whole lot different from the current implementation, or am I missing something? If the player in the room dies, the substance dealing the damage would still think the player is in the room because it will, at least in my case, not receive a OnTriggerExit if I destroy the player object.
Thanks again for your thoughts on the issue, I'd love to "solve" it and get on with the project :)
Well you wouldn't specifically target a Wall type, the object instance would be a Wall but it would be referenced as a base class/interface object like (TargetableObject) on which you can do this like Buff, Damage, Destroy, $$anonymous$$ove. This way anything your enemy targets can be handled by the code.
As for the other comment, the player's object would be destroyed or moved so it wouldn't trigger that method anymore, and you wouldn't need to depend on your OnTriggerExit.
You can't exit the collider because you aren't walking and you don't start walking until you exit the collider, see where the logic breaks?