Anya Mittal
Anya Mittal

Reputation: 1

Trying to destroy prefab upon collision

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.sstatic.net/tz9OU.png)

Would appreciate any help!

Upvotes: 0

Views: 71

Answers (2)

hawk
hawk

Reputation: 1

CapsuleCollider is conflicted with BoxCollider. You should make the size of BoxCollider bigger than CapsuleCollider then try again.

Upvotes: 0

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!

Upvotes: 0

Related Questions