- Home /
Help with OnTriggerExit never being called?
Hi all! I've been working on my first little game for a while, learning Unity and programming along the way. In this game, I have doors that open and close when a 'button' is stood on.
This is achieved using events (the button shoots out the events, which the door subscribes to):
public delegate void Simple();
public event Simple OnPressed = delegate {};
public event Simple OnReleased = delegate {};
void OnTriggerEnter(Collider col)
{
OnPressed();
}
void OnTriggerExit(Collider col)
{
OnReleased();
}
The door then listens to these events, and opens and closes appropriately.
The problem comes when objects standing on the 'button' are destroyed. As the destroyed object never exits the trigger, the function to close the door is never called and therefore remains open.
Surely there must be a better way of going about this entire door trigger system. As I plan on using a similar method on other objects in the game, I'd rather change my entire system sooner rather than later, or learn a good way of working around the problem.
To clarify my goal: I wish to have a button that opens a door when stood on, and closes it when no object is on it.
Any suggestions are welcome, and thanks in advance!
Answer by jtok4j · Nov 21, 2014 at 07:31 AM
Hey,
Why not use "On Destroy" which runs just before an object is destroyed. Use it with the objects which might/are be(ing) destroyed in your game after stepping on that button.
More info here: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnDestroy.html
Leave the current code for live objects, which aren't going to be destroyed, and add this to objects which you know are gonna be destroyed when pressing that button. ( just use the same code: OnReleased(); ) which you're calling now, in the "on destroy" area of these objects.
Questions/Clarifications? Just write a note! :D
Hi jtok! Firstly, thank you very much for your response. I actually stumbled upon the OnDestroy method earlier tonight, but wasn't sure exactly how I'd use it! I'm a little confused by your explanation (sorry!). Here's my interpretation: Create new script that has OnDestroy function. When destroyed, somehow shoot out the OnReleased event? If I'm correct, then how exactly would I have a separate script send an event that already exists in another script? I'm quite new to events. The only way I could think of doing that would be to make the new script of class TriggerButton (which was the name of the script in my original post).
AAAaaahh... Spent a good 8 $$anonymous$$utes writing out my reply, and posted it, only to realize that the "comment" was actually written in an "answer" box... and the forums killed it after re$$anonymous$$ding me to not double-post answers... (head in hands)
Ok, here we go again.... :)
You can simply use this On Destroy bit in the same way that you're using the On Trigger Enter, and On trigger exit parts. (And Community, correct me where I'm off/wrong, please.)
Here's my example in C#:
void OnDestroy() { OnReleased(); }
(Not sure if you'd need the (Collider col) part... weak programmer here.)
Add the above to your current script and make sure that this script is a Component of whatever object you're hoping to call the door-closing part before it's destroyed.
(note that this On Destroy part isn't active/called until the object it's attached to (is a Component of) is actually destroyed by the system)
(Alternatively, a better idea would be to create a separate script (which calls this door-closing function only) and attach it to the objects which need to call the door-closing part, at some point of their life. This helps to clarify what exactly the scripts are doing for an object, and makes things simpler. )
Awww, I'm so sorry about the comment/answer thing >.<
I've done exactly that - made a script of class TriggerButton that has only an OnDestroy function spitting out the OnReleased event:
void OnDestroy()
{
OnReleased();
}
This seems to be okay as far as the compiler is concerned, but as soon as I put it onto a GO in Unity, i get the error: The event 'TriggerButton.OnReleased' can only appear on the left hand side of '+=' or '-=' operator
Any ideas? Thanks again for your time, much appreciated!
Hmmm, I'm not sure... Hopefully someone else will enlighten the both of us. :P
Indeed! :) Thank you so much for your help, the logic behind your answer seems fair, I guess the problem is just something to do with the syntax. Here's hoping for a solution from somebody else!
Your answer
Follow this Question
Related Questions
OnTriggerEnter/Exit called unexpectedly 2 Answers
On trigger exit not working 2 Answers
Help with OnTriggerEnter issue 3 Answers