Destroying objects with the same name that are touching eachother.

Hello

I have already been searching this site and google for related answers, and i have found none, but if anyone can find some, then just link it here and call me a noob :).

Anyway, what i want to do, is to click on a gameobject with a box collider on it, and delete destroy eventual gameobjects which are touching the first gameobject.

i know i can use 'OnCollisionEnter' to destroy objects when they touch eachother, but that happens immediately when they come into contact, i want to wait till a button is pressed.

i have tried making a variable with the name of the gameobjects that are touching the object, but for some reason it didnt work out for me, it deleted the object i pressed, and 1 other object, always the same. but it didnt work as intended, as it didnt matter if the object was touching the object i was pressing.

I hope someone is able to help me, tell me if im rambling, and i'll try to clarify it a bit.

Thanks in Advance.

-Seth

Anyway, what i want to do, is to click on a gameobject with a box collider on it, and delete destroy eventual gameobjects which are touching the first gameobject.

See OnCollisionStay, OnMouseDown.

bool destroy = false; 

IEnumerator OnMouseDown()
{ 
    destroy = true;    // Let object know we should destroy.
    yield return null; // Wait one frame so physics can do its stuff.
    destroy = false;   // Don't allow destruction after one frame.
}

void OnCollisionStay(Collision collision)
{
    if (destroy && collision.gameObject.name == name)
    {
        Destroy(collision.gameObject);
    }
}

Or JS, if you prefer:

var destroy : boolean = false; 

function OnMouseDown()
{ 
    destroy = true;    // Let object know we should destroy.
    yield;             // Wait one frame so physics can do its stuff.
    destroy = false;   // Don't allow destruction after one frame.
}

function OnCollisionStay(collision : Collision)
{
    if (destroy && collision.gameObject.name == name)
    {
        Destroy(collision.gameObject);
    }
}

Note that colliders require a rigidbody or character controller in order for events to fire.