Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by kitallen23 · Nov 21, 2014 at 07:16 AM · triggerontriggerenterontriggerexit

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kitallen23 · Nov 21, 2014 at 08:15 AM 0
Share

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).

avatar image jtok4j · Nov 21, 2014 at 08:30 AM 0
Share

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. )

avatar image kitallen23 · Nov 21, 2014 at 08:44 AM 0
Share

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!

avatar image jtok4j · Nov 21, 2014 at 08:49 AM 0
Share

Hmmm, I'm not sure... Hopefully someone else will enlighten the both of us. :P

avatar image kitallen23 · Nov 21, 2014 at 08:54 AM 0
Share

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!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnTriggerEnter/Exit called unexpectedly 2 Answers

On trigger exit not working 2 Answers

Help with OnTriggerEnter issue 3 Answers

Display gui box after trigger is activated? 2 Answers

Can someone clarifies OnTriggerEnter, OnTriggerExit and OnTriggerStay for me? (I have image included already) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges