OnCollisionEnter twice

Hey guys! :slight_smile:

I’m new in Unity and am watching some videos to learn this engine. It’s not hard, and I’m already doing some nice stuff. :smile:
But I’m breaking my head over here.I made an example so you can see it.

Please take a look in this picture.

That cilinder whith this cube “hat” is my hero. And he hold a weapon with 10 ammo (as you can see in the screen.
That capsule in the right give the weapon 3 ammo if the hero collide it. Everything OK until them. But when I collide…

See? There’s a “double collision”. So the ammo received +6 and not +3;

Here’s the console. You can see he “hit” twice.

Here’s my Recharge script. (Which is in the capsule)

#pragma strict

function Start () {

}

function Update () {

}

function OnCollisionEnter(collision : Collision) {
   
    if(collision.gameObject.tag == "Hero") {
        Destroy(gameObject);
        weapon.contAmmo = arma.contAmmo + 3;
        print("hit");
}
}

Here’s the weapon script

#pragma strict

var bullet: GameObject;
var ammo: UnityEngine.UI.Text;
public static var contAmmo : int;

function Start () {
    contAmmo = 10;
}

function Update () {

    ammo.text = "Ammo: " + contAmmo;

    if(Input.GetKeyDown("space")) {
        if(contAmmo > 0) {
            Instantiate(bullet,transform.position,transform.rotation);
            contAmmo = contAmmo - 1;
        }
    }
}

I know I can solve this putting a flag on the collision detection like this:

if(collision.gameObject.tag == "Hero") {
        if (!used) {
             used= true;
             Destroy(gameObject);
             weapon.contAmmo = weapon.contAmmo + 3;
         }
    }

But I just want to know why this is happening :confused:

Thanks for reading! :slight_smile:

does the cube and cylinder on the hero both have colliders? do they have rigidbodies (if so which ones? both?)

Hey, man. Thanks for your answers :slight_smile:

They have rigidbodies. But I tested something and it’s curious.

I have the speed of the Hero = 4*Time.deltaTime;
It works fine (Ammo just +3)
But when the speed is 5 or more it almost always it’s Ammo +6.
Something about frames? :confused:

change your debug to

"hit" + gameobject.name

I suspect you’re getting a collision off both the cube and cylinder… you really should only have a single rigidbody on a hierarchy of objects (i.e. parent/children/etc.)

I changed the debug and the speed to 10 again. Two collisions registered at the Capsule:/

(P.S.: It’s Cylinder in the print screen, but it’s the capsule here in game, I changed the name of the wrong object)