Collisions not working

Hello, I am a beginner to Unity and C#. I have an enemy and the object the enemy is trying to attack. I have tried to make it so when the enemy collides with this object, it will get destroyed. I have gotten this code from another, more older, thread(which I have modified). If anyone could help, it would be greatly appreciated!

using System.Collections;
using UnityEngine;

public class DestroyOnCollision : MonoBehaviour
{
    void OnCollisionEnter(Collision col)
    {
        if (col.gameObject.tag == "Enemy")
        {
            Debug.Log("collision detected!");
            Destroy(gameObject);
        }
    }
}

Nevermind, I have solved the issue

Review all the requirements in the documentation for OnCollisionEnter. IF you are not meeting any one of them, it won’t work.

Once you’re confident you are meeting all the requirements, put another Debug.Log() statement above the tag check and see if the method is ever getting called.

1 Like

Another thing to check: Is your game 2D or 3D? If you’re using 2D, you’ll need to use the 2D version of the physics callbacks.

1 Like