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 HarrisonJH · Jan 12, 2015 at 11:17 AM · ontriggerenter2d

Unity4.6 OnTriggerEnter2D is not called constantly

My player object has 1 rigidbody2D (not kinematic), 1 circle collider, 1 box collider attached and tagged as "Player". My pickup has a trigger circle collider.

I instantiate several pickups in my scene, some can be picked up but some can't.

This is the code attached on pickups

 void OnTriggerEnter2D (Collider2D other)
     {
         if (other.tag == "Player")
             gameObject.SetActive (false);
     }

If anyone could help, that would be very appreciated.

Comment
Add comment · Show 6
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 NaRango4 · Jan 12, 2015 at 11:57 AM 0
Share

i think you should start debugging, > Check if all of your colliders are 2D > use debug statements, > A gameobject can have only one collider of either normal or 2D, since you are telling you have 2 colliders attached, check which collider has proper tag > use OnTriggerStay2D() for debugging

avatar image HarrisonJH · Jan 12, 2015 at 12:00 PM 0
Share

The sample asset has a 2D character which has 2 colliders too, and it works well. All colliders are 2D, since all objects come from one prefab

avatar image Priyanshu · Jan 12, 2015 at 12:10 PM 0
Share

multiple collider component on same GameObject do not work.

If you want to add multiple colliders on same GameObject . Add them as child of that GameObject.

avatar image HarrisonJH · Jan 14, 2015 at 04:03 AM 0
Share

@NaRango4 @Priyanshu Nothing changes after I remove one of the collider. Some coins are picked up, some are not.

avatar image CodeMasterMike · Jan 14, 2015 at 09:07 AM 0
Share

Does the "Player" also have a OnTriggerEnter2D somewhere? If so, it might be that the "Player" takes the trigger event and therefor the pickup doesnt get the trigger message.

Try something like this, to see if the OnTriggerEnter2D actually gets called when you collide:

 void OnTriggerEnter2D (Collider2D other)
      {
          if (other.tag == "Player")
              gameObject.SetActive (false);
          else
              Debug.Log("Collision but not with player");
      }
Show more comments

1 Reply

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

Answer by SilentSin · Jan 14, 2015 at 10:48 AM

Your title doesn't seem to reflect your actual problem, you don't want OnTriggerEnter to be called repeatedly, just once for every object.

Try adding:

 else
 {
     Debug.Log("WTF did I hit? " + other, other);
     Debug.Break();
 }

The second parameter tells Unity which object to highlight when you click on the log message and the Debug.Break() will pause the game when it happens. So you'll get a log line and single clicking it will highlight the object you hit in the hierarchy.

If it never logs that line, then there must be something different about the pickups its not working on. Are their colliders disabled? Are they on the wrong layer? Are their colliders offset to some weird position?

Comment
Add comment · Show 12 · 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 HarrisonJH · Jan 14, 2015 at 11:10 AM 0
Share

All pickups come from one prefab and dynamically spawned by one script. I can't think of anything reason those pickups won't be identical. Someone else already mentioned your debug code, and as I said, codes in the else statement never got executed

avatar image SilentSin · Jan 14, 2015 at 11:15 AM 0
Share

Why do you have both a circle and a square collider on your player? Are they both on the same object or is one on a child object?

avatar image HarrisonJH · Jan 14, 2015 at 11:23 AM 0
Share

Because I don't konw how to make the character move smoothly by only using a box collider. I have to add a circle collider at the bottom.

The circle collider is in a child object. Actually I tested both situation. When 2 colliders are on the same object, roughly half of the pickups will be missed. Once I move the circle collider to the child object, it reduces to about 2/10.

avatar image SilentSin · Jan 14, 2015 at 11:25 AM 0
Share

Are both the box and circle tagged as player? $$anonymous$$aybe its triggering on the one that isn't tagged then not triggering on the other because they share the same rigidbody?

avatar image HarrisonJH · Jan 14, 2015 at 11:27 AM 0
Share

Yes, they are. If it's triggered by an untagged collider, I would get the debug message.

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

6 People are following this question.

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

Related Questions

Child Trigger event on parent Script 1 Answer

OnTriggerEnter2D Not Working 1 Answer

OnTriggerEnter2D is not being called 1 Answer

score display not showing 1 Answer

Checking 2DCollisions During a single Update 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