Search Unity

OnCollisionEnter not being called on collision

Discussion in 'Scripting' started by DoomDude99, May 29, 2019.

  1. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    There are several similar threads out there

    https://gamedev.stackexchange.com/questions/50770/unity-oncollisionenter-not-called

    , however, I can't see why the OnCollisionEnter method is not being called.

    I have a "Player" game component that must interact with an "Obstacle". In the inspector they look like:

    Player:



    Obstacle:



    The scripts use by "Player" are:

    PlayerMovement.cs:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3.  
    4. public class PlayerMovement : MonoBehaviour
    5. {
    6.     public Rigidbody rigidBody;
    7.     public float forwardForce = 2000f;
    8.     public float sidewaysForce = 500f;
    9.  
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         // rigidBody.useGravity = false;  
    14.         // rigidBody.AddForce(0, 2000, 500);
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void FixedUpdate()
    19.     {
    20.         // Add a forward force
    21.         rigidBody.AddForce(0, 0, forwardForce * Time.deltaTime);
    22.  
    23.         if (Input.GetKey("d")) {
    24.             rigidBody.AddForce(sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    25.         }
    26.  
    27.         if (Input.GetKey("a"))
    28.         {
    29.             rigidBody.AddForce(-sidewaysForce * Time.deltaTime, 0, 0, ForceMode.VelocityChange);
    30.         }
    31.  
    32.     }
    33. }
    34.  
    and PlayerCollision.cs:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class PlayerCollision : MonoBehaviour
    4. {
    5.  
    6.     public PlayerMovement movement;     // A reference to our PlayerMovement script
    7.  
    8.     // This function runs when we hit another object.
    9.     // We get information about the collision and call it "collisionInfo".
    10.     void OnCollisionEnter(Collision collisionInfo)
    11.     {
    12.         Debug.Log("Hit something");
    13.         // We check if the object we collided with has a tag called "Obstacle".
    14.         if (collisionInfo.collider.tag == "Obstacle")
    15.         {
    16.             movement.enabled = false;   // Disable the players movement.
    17.             FindObjectOfType<GameManager>().EndGame();
    18.         }
    19.     }
    20.  
    21. }
    22.  
    Method OnCollisionEnter() is never called. Why?

    GameManager is another component:



    where GameManager.cs is:

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class GameManager : MonoBehaviour
    4. {
    5.     public void EndGame() {
    6.         Debug.Log("GAME OVER MAN! GAME OVER!");
    7.     }
    8. }
    What's wrong with the collision?
     
  2. DoomDude99

    DoomDude99

    Joined:
    May 11, 2019
    Posts:
    87
    I forgot to tag the obstacles ... =)) . Fixed.