Destroy object using mouse click(raycast to detect collision)

I've been in this problem for 2 weeks now and now its pissing me off ~__~

I'm trying to destroy a game object by using mouse click, i have done it using the usual mouseclick destroy game object method. but i want the object to be destroyed when i cliked "on" it.

Raycast probably is the best for it since it's as thin as needle (lol). now i want the raycast to be at my mouse pointer so i can use it to detect collision... can someone help me with this :'(

2 Answers

2

Why not use:

function OnMouseUp ()
{
Destroy(gameObject);
}

When you click the object with this script attached to it, it will destroy the gameobject clicked on.

Or, use this. I am trying to do something very similar:

function OnMouseOver()
{
 if(Input.GetMouseButtonDown(0))
 {
  Destroy(gameObject);
 }
}

is there a collider attatched to your object? if not, and it is imported, just use a mesh collider on it and it should work fine.

Is the script and the collider in the same place on the object hierarchy? If the collidor is on a child object I don't think it will trigger the script.

Also, make sure that it is not set to a trigger.

is the parent object an empty gameObject? in that case, you will need to set a var of the parent object and then tell it to destroy like this: Destroy(parentVarNameHere); .