• 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
Question by alexpseidel · Jul 11, 2014 at 04:21 AM · javascriptontriggerenterontriggerenter2d

onTriggerEnter2D not acting like I thought

Hey, so I was fiddling around with onTriggerEnter2D, and I succesfully wrote a script that tested if a certain collider entered the trigger, and set a static var "collide" equal to true if it did. Here was my original script:

 #pragma strict
 var target : Collider2D; 
 static var collide : boolean;
 
 function OnTriggerEnter2D(collision:Collider2D){
     if(collision == target){
         collide = true;
     }
     else{
         collide = false;
     }
 }
 function Update () {
 }

It worked, and I was able to do stuff only when collide was true in other scripts. However, once the target object left the trigger the collide var was never turned off! This makes sense, of course, since the else - false statement only comes into affect if ANOTHER collider enters the trigger. I decided to make a "called" variable to fix the problem:

 #pragma strict
 var target : Collider2D; 
 static var collide : boolean;
 private var called : boolean;
 
 function OnTriggerEnter2D(collision:Collider2D){
     if(collision == target){
         collide = true;
     }
     else{
         collide = false;
     }
     called = true;
 }
 function Update () {
     if(!called){
         collide = false;
     }
     called = false;
 }

I would naturally thing that this would turn collide off only when there is not a collision with the target. However, when I run the script, my collide script doesn't have any effect. Is there something I'm missing here? Is cross referencing between OnTriggerEnter and Update a bad idea? If so, is there a way to do an if(!OnTriggerEnter) or something of that effect?

Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by Kiwasi · Jul 11, 2014 at 04:24 AM

I'm sorry to tell you, but you've gone a fair ways down the wrong path.

OnTriggerEnter is called once when the collider first enters the trigger. Then it will never run again unless the collider exits the trigger and enters it again.

The effect you are after can be achieved by using a combination of OnTriggerEnter and OnTriggerExit. This is how I prefer to do it. You can also use OnTriggerStay.

Comment

People who like this

0 Show 1 · 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 alexpseidel · Jul 11, 2014 at 02:05 PM 0
Share

Thank you, thank you, thank you! The OnTriggerExit is the answer to the first problem I had (as opposed to the "called" variable). I can just have it turn collide off OnTriggerExit. I can't believe I hadn't thought of that! Many thanks again for answering and for pointing out the other OnTrigger functions I had so carelessly forgotten about.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to use OnTriggerEnter? (JS) 1 Answer

OnTriggerEnter2D Rapid Fire 0 Answers

How can i detect hits from enemies inside of a player? 1 Answer

How to change physic material during game when the player hits an object? 3 Answers

Instatiate inside OnTriggerEnter gives unwanted delay 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