2
\$\begingroup\$

In Unity, the OnTriggerExit() method is not called/fired (not sure if it's an event or not) when an object is disabled or destroyed within it's collider. In comparison to it being fired is an active object that was within it's collider no longer being inside the bounds of that collider on the next frame. I want to be able to call OnTriggerExit() on my triggers OnDisable() method as a workaround.

How would I go about calling/firing OnTriggerExit() manually?

\$\endgroup\$
2
\$\begingroup\$

If you want the code in your OnTriggerExit to be called inside the OnDisable method as well, simply encapsulate the code into a function of its own, and call the function from both methods.

void OnTriggerExit(Collider other) {
    CommonTriggerExitCode();
}

void OnDisable() {
    CommonTriggerExitCode();
}

void CommonTriggerExitCode() {
    //Code to be called when trigger exits or object is disabled
}

As far as I know, there's no way to manually trigger these events, and this strategy is far less of a work-around than manually triggering events.

| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Here is a simple solution:

Instead of calling Destroy(yourObject) everywhere , replace that with a CustomDestroy function what has the following code:

public static void CustomDestroy(GameObject o)
{
    o.transform.position = new Vector3(100000000.0f,10000000.0f,10000000.0f);
    Destroy(o);
}

Setting the position before you destroy an to 'infinity' will handle all the appropriate collisions calls.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.