Search Unity

OnCollisionEnter not working

Discussion in 'Scripting' started by Mike L, Aug 3, 2011.

Thread Status:
Not open for further replies.
  1. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    for some reason, this isn't working
    Code (csharp):
    1. void OnCollisionEnter (Collision hit) {
    2.     //if (hit.impactForceSum.magnitude > 10) {
    3.         Debug.Log("yay");
    4.     //}
    5. }
    its not registering the collision and saying yay in the console

    Edit: i forgot to tell you, its a capsule collider, and isTrigger is false
     
  2. zine92

    zine92

    Joined:
    Nov 13, 2010
    Posts:
    1,347
    Is your other collider having rigidbody or any rigidbody attached to this collider. On colllision will only be called when one of the colliders have a rigidbody. Take a look at this. OnCollisionEnter.
     
    Last edited: Aug 3, 2011
  3. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    ah, thats what is missing, thanks man
     
  4. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    new problem, now its colliding with something i really dont want it to, ill try something and see if it works in the future...
     
  5. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
  6. Mike L

    Mike L

    Joined:
    Sep 14, 2010
    Posts:
    1,035
    Yeah, i know about that, but if i try that, that takes more time to set up, i just decided to do it a bit differently
     
  7. Rafes

    Rafes

    Joined:
    Jun 2, 2011
    Posts:
    764
    If you set the other collider to be a trigger only, it should work, if I understand your issue correctly. That is what the isTrigger is for.
    You may also need to set the new collider to isKinematic, though I don't see that changing the way the collider works, just to stop it from being processed by PhysX.
     
  8. msue2

    msue2

    Joined:
    Jun 2, 2017
    Posts:
    12
    Another possible issue: The order of Rigidbody and Collider components on the game object. This requirement is not documented on the OnTriggerEnter page. Tested on 2017.4.

    The Rigidbody component must be added first, followed by the Collider component. The order of already added components can be changed in the inspector cogwheel dropdown with "Move Up"/"Move down".
     
  9. megabrobro

    megabrobro

    Joined:
    Jul 8, 2017
    Posts:
    109
    You are a hero today sir
     
    GhostSlayer44 likes this.
  10. rodrigoschmidtgodinho

    rodrigoschmidtgodinho

    Joined:
    Apr 9, 2020
    Posts:
    1
    your collider is too small. too small.Increase your size.
     
  11. ApocalypseZ

    ApocalypseZ

    Joined:
    Apr 21, 2019
    Posts:
    1
    Might be a silly answer, but sometimes the little things are missed.

    I ended up on this post and couldn't figure it out. Come to find out, I forgot to attach the script to my projectile gameObject.

    Make sure your script is attached! =D
     
  12. dkaloger

    dkaloger

    Joined:
    Jul 7, 2019
    Posts:
    39
    also make sure the rigidbody is not kinematic
     
  13. Grant-king7432

    Grant-king7432

    Joined:
    Jan 25, 2020
    Posts:
    1
    Hey all, I have been having similar issues and found this page. For me I had a prefab that had child objects(Camera, Lamp, Cylinder). I had my script on the Parent prefab and OnCollisionEnter was not registering....It turns out the Rigidbody was not registering with the floor because the Ridgidbody was on the Cylinder child object....not the parent prefab. Even with dragging the Cylinder Child object into the inspector as the Rigidbody to use, still no register with the ground....I put a small script on the Cylinder Child object and dragged in the same Rigidbody and walla....Now it is registering as touching the ground.
     
  14. steveh2112

    steveh2112

    Joined:
    Aug 30, 2015
    Posts:
    314
    i know this is old but i just found a solution to a problem in my code, i had OnCollisionEnter in a script attached the parent of the thing that was getting the collision, not the thing that was getting the collision, so it didn't work.

    obvious really but if you are getting frustrated, check you have the script attached to the correct object
     
    taylor_unity867 likes this.
  15. Ryker404

    Ryker404

    Joined:
    Jul 8, 2020
    Posts:
    8

    Thank You!
     
  16. B7LLF

    B7LLF

    Joined:
    Mar 28, 2021
    Posts:
    1
    I just had this when following a tutorial, turns out that the gameObject.name is important and needs to match your object that is doing the collision ('player-ball' in my case). I had just typed the name in wrong.
     
    charmseer likes this.
  17. shaktip2014

    shaktip2014

    Joined:
    Nov 15, 2021
    Posts:
    1
    i just want to say. Love u bro
     
  18. jakim28

    jakim28

    Joined:
    Dec 21, 2021
    Posts:
    1
    None of these work for me :(
     
  19. JakubJWFA

    JakubJWFA

    Joined:
    Dec 23, 2021
    Posts:
    1
    Another possible issue, even if a bit silly:
    If you make 2D game make sure you use void OnCollisionEnter2D(Collision2D collision) not OnCollisionEnter.
     
    Last edited: Dec 27, 2021
  20. dhtpdud528

    dhtpdud528

    Joined:
    Apr 8, 2018
    Posts:
    23
    I'm the luckiest person in the world.
    because This is the only article you have written. and it has been less than 24 hours since you wrote it.
    For me, this is a very simple and accurate diagnosis.

    You saved my time. TY.
     
    Last edited: Dec 28, 2021
  21. AttemptingToMakeAGame

    AttemptingToMakeAGame

    Joined:
    Jan 18, 2022
    Posts:
    1
    This was the solution that actually worked for me thank you!!!!!!!
     
Thread Status:
Not open for further replies.