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:
Would appreciate any help!
](https://i.stack.imgur.com/twV81.png)
I think that the problem is that you have both the
BoxColliderand aCapsuleColliderattached. Try removing or disabling theCapsuleColliderto check if this is the caseIf 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 twoOnCollisionEnter()scripts needed to destroy each other, but only one got destroyed.Hope this helps!