Temp Id
Temp Id

Reputation: 229

OnCollisionEnter() not working in Unity3D

I have an object with a mesh collider and a prefab with sphere collider. I want the instance of the prefab to be destroyed if the two collide.

I wrote the following in a script:

private void OnCollisionEnter(Collision c)
{
    if (c == target)
        Destroy(transform.gameObject);
    print("something");                   // Doesn't get printed
}

But it is not working. I have tried toggling isTrigger on both the objects.

Upvotes: 20

Views: 106038

Answers (6)

Donald Faulknor
Donald Faulknor

Reputation: 37

You cannot under any circumstances enter a collider if is trigger is set to false. It's impossible. You will be stopped before entering the collider.

Upvotes: 0

Ohad Cohen
Ohad Cohen

Reputation: 6144

Have a look at this table

If you want your OnCollisionEnter to be called make sure:

(a) Both objects have a collider attached.

(b) None of the objects is a trigger collider (this will issue OnTrigger function & not OnCollisionEnter)

(c) One of the objects (doesn't matter which of them) is a rigid, non kinematic & non static object (the second don't have to be a rigid body).

(d) Due to computational difficulties MeshCollider might have hard times colliding with other mesh collider, use them with caution.

(e) Make sure both the objects are in the same layer (or at least that they collide in scene settings).

(f) If you are working in 2d - OnCollisionEnter2D will be called, rename your function.

example colider configuration

Upvotes: 47

Matt Baeriswyl
Matt Baeriswyl

Reputation: 61

Have you tried using the OnTriggerEnter() method and setting a collider on the object to a trigger?

If it doesn't need to tell what object its colliding with you could do a simple

void OnTriggerEnter(){
    Destroy(transform.gameObject);
}

Edit:

Also I have done OnCollision like this

private string hitobject;

void OnCollisionEnter(UnityEngine.Collision hit)
{
    hitobject = hit.gameObject.tag;
    if(hitobject == "Plane")
    {
        isgrounded = true;
    }
}

None of the objects are triggers and they don't need rigid bodies to work.

Upvotes: 2

Karessa Torgerson
Karessa Torgerson

Reputation: 21

I had a similar problem. The box collider wasn't as big as the collision object. Setting the x and z values to 2 units fixed the problem!

Upvotes: 2

Peter de Rivaz
Peter de Rivaz

Reputation: 33499

I had the same problem of OnCollisionEnter not being called and found this question.

For me, the problem was that I was making a 2D game so the answer is to use the OnCollisionEnter2D function instead.

Upvotes: 53

lukegravitt
lukegravitt

Reputation: 902

Make sure one of them has a non-kinematic rigidbody attached. Taken from the Unity docs:

When a collision between two Colliders occurs and if at least one of them has a Rigidbody attached, three collision messages are sent out to the objects attached to them. These events can be handled in scripting, and allow you to create unique behaviors with or without making use of the built-in NVIDIA PhysX engine.

From here: Unity3D MeshCollider

Upvotes: 9

Related Questions