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 /
avatar image
0
Question by Wereb · Dec 18, 2018 at 06:21 PM · collisioncolliderscollision detectioncollisionscollisiondetection

How to make a certain function if an object collides with another object? Been searching it up for 2 months.

idk what is wrong with the code:

 void OnCollisionEnter(Collision col)
 {
 
         Debug.Log("Collided");
 
 }

it doesn't work for some reason. And I also need to check "if (col.collider.gameObject.tag == "red")", but I can't as it doesn't even send the message to the console, it doesn't work.

Some details:

One of my objects is set to dynamic, but the other one - to kinematic. I'm using Unity UI and a canvas. That script is on the dynamic object.

I've been searching for solutions, reading a lot of posts for a little over 2 months now. Even took a break for 1 month cause this is just cancer. Should I just suicide at this point lol

Comment
Add comment · Show 4
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 Hellium · Dec 18, 2018 at 06:33 PM 0
Share

Did you read the documentation about kinematic parameter???

If is$$anonymous$$inematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore

So obviously, the collision messages won't be sent.


Without mentionning that UI elements are not meant to use Physics engine. If you really need it, use the 2D Physics engine by using 2D colliders, 2d rigidbodies and the 2D callback functions.

avatar image Wereb Hellium · Dec 18, 2018 at 07:23 PM 0
Share

well,

"Note: Collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached."

It said one of the colliders, but I'll try try to remove another one also, thanks for the reply

EDIT: Still didn't work.

avatar image vlab22 · Dec 18, 2018 at 08:02 PM 0
Share

Are your colliders "is Trigger"? One "is trigger" and another one is collider? Are these objects in layers that match in Physics Table? ( Edit > Project Settings->Physics)

Read carefully: https://docs.unity3d.com/$$anonymous$$anual/CollidersOverview.html Section Collision action matrix

avatar image Wereb vlab22 · Dec 21, 2018 at 05:49 AM 1
Share

Thanks for the reply. Yup one "is trigger" dynamic, another default dynamic and both layers are UI. Still doesn't work.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by JonPQ · Dec 18, 2018 at 08:11 PM

both objects must have rigid bodies attached, and at least one must be non kinematic. also try putting your script on both objects... see if only one is getting the message. Also try adding OnCollisionStay() with a log message in case they are already colliding.

See unity docs https://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html

also try this tutorial https://unity3d.com/learn/tutorials/topics/physics/detecting-collisions-oncollisionenter

Comment
Add comment · Show 3 · 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 Wereb · Dec 21, 2018 at 05:59 AM 0
Share

Thanks for the reply. Both have rigid bodies, both are dynamic, I tried putting ontriggerenter; ontriggerstay; oncollisionenter; oncollisionstay on all of my obj, but it still didn't work.

avatar image JonPQ · Dec 21, 2018 at 06:04 PM 0
Share

you say both layers are Ui... just a thought.... are you trying to collide them in 2d ? view the scene in 3d mode.. make syre they are at same Z and colliding in all 3 dimensions...

avatar image JonPQ · Dec 21, 2018 at 10:54 PM 1
Share

...also check your edit->project settings->physics settings... it has a bunch of check-boxes showing which layers will collide with other layers... You might have Ui disabled.

also why not grab some free demos/learning resources with collisions working... plug in your models... check what is different. https://unity3d.com/learn/tutorials/s/physics

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

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

Player registering collisions where there are none 0 Answers

Flying Controls Collision Issues 0 Answers

Why there is no Collider.IsTouching(...) ? 2 Answers

How does Unity detect collision? 0 Answers

More realistic physics? 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