Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Bindlestick · Apr 20, 2017 at 07:46 PM · gameobjectoncollisionentercollision2distrigger

How to have an object with rigidbody touch another object with isTrigger true

Hello all!

I'm having a bit of an issue with two objects hitting each other. I am probably not doing this the correct or the most efficient way, but I have two objects. One (which is object pooled) moves along the X axis, deleted when it gets to a certain point, and re-spawned. Kind of like a conveyor belt. This object only has a box collider with isTrigger checked. The other object is below it and has both a box collider and a rigidbody. This moves on the y axis.

When I test it out, it passes through, which is what I want, but it doesn't call the onCollisionEnter2d function.

Is there a way for these two objects to phase through each other, and still alert the functions?

     void OnCollisionEnter2D (Collision2D other) {
             if (this.gameObject.tag == "conveyorItem")
             {
                 Debug.Log("CorrectTag");
                 if (other.gameObject.tag == "shootItem")
                 {
                     Debug.Log("Hit!");
                 }
              } 
 }
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
1
Best Answer

Answer by Matthewj866 · Apr 20, 2017 at 08:57 PM

Hi there @Bindlestick

Unfortunately due to the collision type, OnCollisionEnter2D will never be called - instead, you should either:

make the one with OnTriggerEnter2D one call a public method on the other object, e.g.:

  if (other.tag == "whatever the tag is called")
 {
     other.GetComponent<OtherMonoBehaviour>().DoSomething();
 }

OR

Convert both into regular colliders so OnCollision2D() gets called, also change the OnTriggerEnter2D() call on the object with the trigger collider to OnCollision2D() to account for the collider type change.

Hope this helps!

Comment
Add comment · Show 2 · 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 Bindlestick · Apr 21, 2017 at 12:33 AM 0
Share

Thanks for your reply @$$anonymous$$atthewj866. Appreciate the help!

Just making sure I'm understanding correctly, since I am still very green at Unity. The first option, when you say onTriggerEnter2D, am I replacing the onCollisionEnter2D with onTriggerEnter2D?

avatar image Matthewj866 Bindlestick · Apr 21, 2017 at 03:21 AM 0
Share

The first option requires no method name change, assu$$anonymous$$g OnTriggerEnter2D() is already being called by the $$anonymous$$onoBehaviour with access to the trigger collider. If you have defined the method with the wrong name, then you are correct - you will need to change the name to OnTriggerEnter2D().

Documentation: https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter2D.html

Hope this solves your problem! And please remember to mark this as the correct answer if it does.

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

118 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 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 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 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 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

Difficulty setting script on gameObject from another script. 0 Answers

My collision is not being detected or does not produce the desired outcome. 2 Answers

Unity bug gives false error messages or doesen't work without a Debug.Log() line present 1 Answer

When a collider enters a trigger 1 Answer

How to connect onCollisionEnter with another rigidbody via tag,Problem with collision touching a rigidbody 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