Destry Object A When Hit Object B?

Hi, I need help whit my script…I need to Destroy gameobject whit name “floorhold” if I hit gameobject whit tag “floor”.How Can I destroy “floorhold” if I hit “floor”.

My script is:

function OnCollisionEnter(hit : ControllerColliderHit)

{
if(hit.gameObject.tag == “floor”)

{
Destroy(gameObject.Find(“floorhold”));

}
}

I tried this but nothing happend.

I see a problems with your code.

you should capitalize GameObject.Find. gameObject is refer to the object itself. GameObject refers to all the gameObjects that are currently in hierarchy. So here is what you should write :

Destroy (GameObject.Find("floorhold"));

so you change from gameObject to GameObject.

Hope this solves your issue. :)

I have two points to say about your code.

The first point concerns the method OnCollisionEnter : it takes a Collision object.
The second point is that you know which object to destroy, so why don’t you link it with the editor ?

var objectToBeDestroyed:GameObject = null // link it in the editor
function OnCollisionEnter(collision:Collision)
{
    if(collision.gameObject.tag == "floor")
    {
        if(objectToBeDestroyed)
            Destroy(objectToBeDestroyed);
    }
}

That should do it !

Well, there are already two answers which say basically the same thing (and there was another one, which also said the same thing, but whose posted deleted it after it got voted down) so I'm going to go out on a limb here and assume something about what RockDude actually wants to do here.

I think RockDude wants to destroy an object when said object collides with some other object. I might be wrong, but it just keeps bugging me.

The important thing to remember here, is that for a collision to be recognised by the Physics engine, there needs to be at least one rigidbody involved. In this case, I am assuming that the floor is a static collider of some kind, and the object colliding with it will be moving. For this code to work, the "floorhold" object also needs to have a rigidbody component attached, and the rigidbody cannot be kinematic.

EDIT: WELL SCRAP THAT!

I was wrong, the first three posters were right. I'm keeping this post up because the last bit should still be helpful- as far as I can tell, problems start to occur when the OnCollisionEnter is not even being called! Try putting some Debug.Log statements around the place, they can be invaluable tools for finding problems.