Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

OnCollisionEnter not called!!!!

Discussion in 'Editor & General Support' started by metylbk, May 29, 2011.

  1. metylbk

    metylbk

    Joined:
    Apr 19, 2011
    Posts:
    34
    Hi guys!
    I have 2 Objects with box collider and rigidbody attached. I use standard asset script: DragRigidbody.js to drag Object1 to move to collide to Object2. But the function OnCollisionEnter never be called. What I was wrong about?
    function OnCollisionEnter(collision : Collision)
    {
    Debug.Log("Collision Entered");
    }
     
  2. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
    Metylbk, i think you must add a condition event :

    Code (csharp):
    1.  
    2. function OnCollisionEnter (collision : collision){
    3. if (collision.gameObject.name == "the object to hit to");
    4.   Debug.Log("Collision Entered");
    5.   }
    6. }
    7.  
     
  3. powerbears

    powerbears

    Joined:
    May 22, 2011
    Posts:
    2
    doesn't work for me neither, even with the condition

    function OnCollisionEnter(collision:Collision){
    if(collision.gameObject.name=="NME"){
    Debug.Log("Hit");
    Destroy(gameObject);
    }
    }

    the script is attached to an instantiated object (bullet) but it doesn't work :'(
     
  4. U7Games

    U7Games

    Joined:
    May 21, 2011
    Posts:
    943
  5. powerbears

    powerbears

    Joined:
    May 22, 2011
    Posts:
    2
  6. metylbk

    metylbk

    Joined:
    Apr 19, 2011
    Posts:
    34
    Thanks Mig. It worked!
     
    Last edited: May 30, 2011
  7. limdingwen

    limdingwen

    Joined:
    Jun 9, 2011
    Posts:
    25
    Sorry to bump this post but I have the same problem, and I found that it cannot work on dynamically instantiated objects. Help on a workaround?

    Thanks!
     
  8. limdingwen

    limdingwen

    Joined:
    Jun 9, 2011
    Posts:
    25
  9. bandingyue

    bandingyue

    Joined:
    Nov 25, 2011
    Posts:
    131
    friend limdingwen

    can you cut a screenshots?
     
  10. Sep200

    Sep200

    Joined:
    Jul 29, 2013
    Posts:
    13
    Its just not working, even if I have a collider set to trigger -.-

    function OnCollisionEnter(theCollision : Collision){
    if(theCollision.gameObject.name == "Cube"){
    Debug.Log("it works");
    }
    }
     
  11. jayasurya_j

    jayasurya_j

    Joined:
    Feb 28, 2016
    Posts:
    1
    I had the same problem, if you use 2d objects use OnCollision2D function. Let me know if that solved your problem
     
  12. iomac

    iomac

    Joined:
    Mar 26, 2013
    Posts:
    9
    Just in case someone else comes across this, the 2D method is
    OnCollisionEnter2D not OnCollision2D (as of 5.4)