Trying to destroy prefab upon collision

37 Views Asked by At

I'm making a 3D Unity game and trying to get randomly-spawned prefabs disappear when they collide with the player object, but I'm not sure why it isn't working.

private void OnTriggerEnter(Collider col)
{
    if (col.gameObject.CompareTag("target"))
    {
        score.Value += 1;
        Destroy(col.gameObject);
    }
}

I've dragged the script into the player object, and the prefab looks like this:

[enter image description here](https://i.stack.imgur.com/tz9OU.png)

Would appreciate any help!

1

There are 1 best solutions below

0
maks_the_chocolate_bar On

I think that the problem is that you have both the BoxCollider and a CapsuleCollider attached. Try removing or disabling the CapsuleCollider to check if this is the case

If everything looks okay, just add some print("Collided") to the function to check if it even detects the collision. Maybe there are other scripts that iterfere with this. I once had a problem that two OnCollisionEnter() scripts needed to destroy each other, but only one got destroyed.

Hope this helps!