Collision Problem

Edit: Using JavaScript

Trying to fire a bullet which should destroy a box. I don’t necessarily need an animation, I just want the box to disappear. I attached this script to a bullet prefab which spawns an object called Bullet(clone)

var Box : String = “Box”;

function OnCollisionEnter(theCollision : Collision){
if(theCollision.Rigidbody.name == Box){
Destroy(theCollision.Rigidbody);
}
}

I’ve tried this without the var Box : String = “Box”; bit, I’ve tried switching it up so that “Box” has the code on it and checks for a collision with “Bullet(clone)”, and no matter what, the box never disappears. Also tried doing it with Gameobject instead of Rigidbody. Still no dice. What did I do wrong?

Try to debug, just before the Destroy line, write :

Debug.Log("destroyed");

check if console shows you the “destroyed” string…

No string shows up, but it does say there’s a script error.

Script error: OnCollisionEnter
This message parameter has to be of type: Collision

var Box : String = "Box";

function OnCollisionEnter(theCollision : Collision){
   if(theCollision.gameObject.name == Box){
     Destroy(theCollision.gameObject);
   }
}

Destroy(theCollision.rigidbody) just destroy the rigidbody of the object, not the game object itself. You should use Destroy(theCollision.gameObject) instead.

Be also sure that both the bullet and your box has Colliders, and none are triggered.

Hope it helped ; )

Both of them have colliders, neither are triggered. Replaced rigidbody with gameObject, still no dice.

EDIT:

Okay, so I figured out a problem.

function OnCollisionEnter(theCollision : Collision){
if(theCollision.GameObject.tag == “bullet”){
Destroy(theCollision.GameObject);
}
}

That’s my current code. I just learned how to use tags, so hey, working problems out. Now I get this error:

NullReferenceException: Object reference not set to an instance of an object

What to do?

Edit2: Tried a workaround using a variable and some different code. Still no dice.

private var boxdead = false;

function OnControllerColliderHit(hit : ControllerColliderHit)
{
if(hit.GameObject.tag == “bullet”){
boxdead = true;
}
}

function LateUpdate()
{
if(boxdead)
{
Debug.Log(“destroyed”);
Destroy(GameObject.find(“Box”));
}
}

Still no box disappearing, nothing pops up with the debug bit in there. This box just refuses to die.