• 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 jonhillman · Sep 04, 2011 at 04:03 AM · javascriptontriggerexit

OnTriggerExit not working reliably

Can anyone spot why this script might not be working? I have a Sphere collider on a gameobject that has this script attached, and it is colliding with a mesh collider on a gameobject that has a non-kinematic rigidbody, as mentioned in the documentation. OnTriggerEnter seems to work all the time, whereas OnTriggerExit might work once or twice, then cease functioning. I've looked at using OnCollisionEnter/Exit instead, but that seems to not work with colliders as triggers.

 var base : Component;
 var touching : int = 0;
 
 
 function Start(){
     base = transform.parent.GetComponent("baseEdit");
 }
 
 function OnTriggerEnter(other : Collider){
     if(other.name=="terrain line"){
         touching = 1;
         Debug.Log(other.name);
             if(transform.name=="leftAnchor"){
                 base.leftTouching = 1;
             }
             else{
                 base.rightTouching = 1;
             }
     }
 }
 
 function OnTriggetExit(other : Collider){
     if(other.name=="terrain line"){
         touching = 0;
         Debug.Log(other.name);
             if(transform.name=="leftAnchor"){
                 base.leftTouching = 0;    
             }
             else{
                 base.rightTouching = 0;
             }
     }
 }
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by aldonaletto · Sep 04, 2011 at 10:58 AM

OnTriggerExit really doesn't work in this script, and the reason is a typo: you've wrote OnTrigge[t]Exit instead of OnTrigge[r]Exit! But don't feel ashamed: I looked for errors in the code dozens of times (like I guess you did too) and found no errors! I only found the son of a bitch when I decided to write a second OnTriggerExit function - and to my big surprise it worked fine, and Unity didn't complain about your OnTriggetExit...

Comment
Add comment · 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
0

Answer by jonhillman · Sep 04, 2011 at 02:21 PM

Wow, silly mistake. I've rewritten that script a dozen times, trying to make things work, switching to OnCollisionExit, etc...I will see if it works now. Meanwhile, I found another way to get the info I wanted- I'm comparing the position.y of an 'anchor point' (empty game object placed as child on another object) to the nearest position.y on a spline which is terrain (RageSpline...awesome add-on by the way). I'm rotating a 'base' on terrain to match the normal of the collision point with the spline (terrain)...and I have anchors on left and right to determine if they are above/below the terrain, which would mean the base doesn't fit the curvature of the terrain at that position (a level editor of sorts). So I'm wondering if it's cheaper to do that comparison of positions, or if colliders on the anchors (the original idea) is cheaper...any comments?

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

OnTriggerEnter function in c# 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

OnTriggerEnter only recognised once, no matter how many times I enter ? (Solved) 1 Answer

OnTriggerExit issue 1 Answer

Setting Scroll View Width GUILayout 1 Answer


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