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
-1
Question by PushkarSawant97 · May 22, 2014 at 08:33 AM · 2dcollisionontriggerenterontriggerenter2d

OnTriggerEnter2D Doesn't work? ;_;

I AM F*****G INFURIATED BECAUSE OF THIS.

I have a gameObject that has a trigger collider2D attached to it and it uses the onTriggerEnter2D function for detecting objects in its vicinity and onTriggerExit2D function for detecting when objects are not in its vicinity. This is an enemy gameObject and it functions with AI.

The problem is, if it detects an object in its vicinity "objectInVicinity" boolean becomes true, but when that objects leaves the vicinity, the "objectInVicinity" boolean is never set to false.

IT HAS A RIGIDBODY2D ATTACHED TO IT, IT IS ACTIVE, IT HAS COLLISION DETECTION ENABLED AS WELL. I DON'T SEEM TO UNDERSTAND WHAT THE PROBLEM IS.

Another object, which is the player game object, implements the same functions, it is pretty much like the above gameObject mentioned but the code works perfectly fine in this one. WHY THIS ARBITRARY BEHAVIOUR UNITY?

Comment
Add comment · Show 3
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 Andres-Fernandez · May 22, 2014 at 08:56 AM 0
Share

Can you post your code? $$anonymous$$aybe there's something that switches layers or anything...

Does the player has the same script attached?

avatar image Maerig · May 22, 2014 at 08:59 AM 2
Share

Set the boolean to false in OnTriggerExit2D ?

avatar image PushkarSawant97 · May 22, 2014 at 10:52 AM 0
Share

That's basically what I did. Still doesn't work.

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunnybomb7670 · May 22, 2014 at 10:01 AM

Are you naming the function OnTriggerEnter2D ? it HAS to be the correct CASE because otherwise it wont run.

Comment
Add comment · 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 PushkarSawant97 · May 22, 2014 at 10:51 AM 0
Share

Yes, I have named everything correctly. Not trying to sound cocky but I'm pretty conversant with OnTrigger functions in both 2D and 3D and also collider related functions. Like I said, it works for the enter 2D but it never goes back to false when I call the exit 2D function. Also, why I don't use OnTriggerStay2D, that's because that doesn't work at all.

avatar image
0

Answer by PushkarSawant97 · May 22, 2014 at 10:56 AM

var objectInReach : boolean; //This is the boolean I want to change.

function OnTriggerEnter2D(other: Collider2D) {

   if(other.gameObject.tag == "Object") {
      objectInReach = true;
   }
   

}

function OnTriggerExit2D(other : Collider2D) {

   if(other.gameObject.tag == "Object") {
      objectInReach = false;
   }
   

}

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 pumpkinszwan · May 22, 2014 at 12:32 PM

OnTriggerExit2D doesn't work reliably for me AT ALL.

I have removed it from all my scripts and replaced it with a workaround. In a nutshell I store any items that collide with 'X' in a list during OnTriggerEnter2D & OnTriggerStay2D. I then enact whatever I need to on that list of objects. Then after every frame I clear that list. Repeat. Only items that are colliding in any given frame actually get affected because I'm clearing the list every frame.

Here's an example:

 void OnTriggerEnter2D(Collider2D other)
 {
     objects.Add(other);
 }
 
 void FixedUpdate()
 {
     foreach (Collider2D other in objects)
     {
         other.attachedRigidbody.AddForce(new Vector2(5,5));
         // you could set a flag value here to 'true'                                           
     }
     objects.Clear();
     // or clear any flag indicating the objects are touching - flag will be re-added on the next frame if they are still touching
 }

Note that I have only been testing my project on Windows 8.1 and Windows Phone 8, so it's possible that the issue is platform-specific. What platform(s) are you testing on?

Another approach that works for some things is to enact your collision logic in the OnTriggerStay2D, as I find this doesn't trigger even when the OnTriggerExit2D hasn't fired if the two items no longer touch.

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 2dfruity · May 22, 2014 at 12:30 PM

try it with 3D colliders. sometimes the 2D ones don't work for me, but they should do the same thing regardless. have to make them both 3D colliders though.

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.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

OnTriggerEnter2D on an empty game object with a box collider? 1 Answer

Double-Counted OnTriggerEnter2D Events: Why? 0 Answers

OnTriggerEnter detection too late 0 Answers

My OnTriggerEnter2D(Collider2D other) not working 1 Answer

OnTriggerEnter2D not working, alternatives? 2 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