Search Unity

OnCollisionEnter not working in Unity 3D

Discussion in 'Scripting' started by yashi393, Jan 21, 2018.

  1. yashi393

    yashi393

    Joined:
    Jan 21, 2018
    Posts:
    4
    Collision b/w a primitive sphere and cube. No debug message. Now, before you say to check similar posts...

    Both have rigidbodies.
    Both have colliders slightly larger than themselves.
    Both have no trigger (need physics) and are non-kinematic.
    No high speed collisions. I can clearly see the two objects colliding.
    Referencing object name. I'm not aware if using any layers.
    The "enemies" are generated in game, gravity is on, and collision is discrete, if any of that matters.
    Edit: Just regular objects, not children of anything. Nothing disabled. Debug messages from other scripts appear.

    //code on the player (cube) and no collision code on "enemies" (spheres)
    void OnCollisionEnter (Collision col) {
    if (col.gameObject.name == "bigEnemy") {
    Debug.Log ("hit by bigE");
    } else if (col.gameObject.name == "runtEnemy") {
    Debug.Log ("hit by runtE");
    }
    }

    Any help appreciated!
     
    Last edited: Jan 21, 2018
  2. Laperen

    Laperen

    Joined:
    Feb 1, 2016
    Posts:
    1,065
    have you tried putting a print statement outside the if/else statement just to see if the function is being fired?
     
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    Do the names match exactly? Caps match and no additional stuff like the (clone) that gets added on?
     
  4. yashi393

    yashi393

    Joined:
    Jan 21, 2018
    Posts:
    4
    Thanks for the replies. I realized I didn't attach script to the player :mad: