x


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 :'(

more ▼

asked Mar 12, 2011 at 01:13 PM

Jopz gravatar image

Jopz
4 13 11 13

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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

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

answered Mar 12, 2011 at 01:37 PM

zmar0519 gravatar image

zmar0519
1k 165 127 143

thanks for the codes... but it seem not to work... i've tried it in a dummy scene. yeah it works, but when i'm using it on imported game objects it can't seem to work... x.x

Mar 12, 2011 at 03:40 PM Jopz

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.

Mar 12, 2011 at 04:02 PM zmar0519

it do have mesh collider, but it still can't seem to work x.x

Mar 12, 2011 at 04:48 PM Jopz

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.

Mar 12, 2011 at 06:51 PM NewfieJoe

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

Mar 13, 2011 at 12:05 AM zmar0519
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Mar 12, 2011 at 01:16 PM

OrangeLightning gravatar image

OrangeLightning
6.6k 127 120 174

yeah... it seems to be working when i'm using it on a dummy.. like simple cube... but when i'm using it on imported game objects it can't seem to work... x.x

Mar 12, 2011 at 03:02 PM Jopz
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1802
x1472
x1261
x368
x25

asked: Mar 12, 2011 at 01:13 PM

Seen: 6247 times

Last Updated: Mar 12, 2011 at 01:13 PM