OnCollisionEnter not working

Here is my code:

private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Item") 
        {
            Debug.Log("Collided");
            if (inventory.AddItem(collision.gameObject.GetComponent<ItemScript>())) 
            {
                Destroy(collision.gameObject);
            }
        }
    }

The item i’m trying to collide with has the correct tag and also has a rigidbody attached to it. The debug doesn’t even get called when I collide.

Double check a couple things.

  • OnCollisionEnter is for Collision
  • OnTriggerEnter is for a Collider with
    “is Trigger” enabled.
  • If you are detecting 2D collisions
    use: OnCollisionEnter2D,
    OnTriggerEnter2D
  • Make sure you have spelled the “tag”
    variable correctly, it is
    case-sensitive. Also make sure you
    have added the tag to the correct
    object.

If the above still doesn’t work then maybe your collisions are inconsistent? Your objects may not be colliding if they both have a similar boxCollider.

Try putting a slightly larger BoxCollider set as Trigger and then put your above code in a OnTriggerEnter method.

Note: Using “private” as an access modifier is correct and will work.

If things still don’t work, then it could be that you have attached the wrong Components and are checking your collisions in the wrong object?

Post code and images of your setup if it still doesn’t work after doing the above.