• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Jopz · Mar 12, 2011 at 01:13 PM · objectmousedestroyclickusing

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

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
Best Answer

Answer by zmar0519 · Mar 12, 2011 at 01:37 PM

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

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

People who like this

0 Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jopz · Mar 12, 2011 at 03:40 PM 0
Share

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

avatar image zmar0519 · Mar 12, 2011 at 04:02 PM 0
Share

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.

avatar image Jopz · Mar 12, 2011 at 04:48 PM 0
Share

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

avatar image NewfieJoe · Mar 12, 2011 at 06:51 PM 0
Share

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.

avatar image zmar0519 · Mar 13, 2011 at 12:05 AM 0
Share

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

Show more comments
avatar image

Answer by FLASHDENMARK · Mar 12, 2011 at 01:16 PM

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.

Comment

People who like this

0 Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jopz · Mar 12, 2011 at 03:02 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

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

Update about the future of Unity Answers

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta later in June. Please note, we are aiming to set Unity Answers to read-only mode on the 31st of May in order to prepare for the final data migration.

For more information, please read our full announcement.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Destroy objects by clicking on them 1 Answer

OnMouseUp() Click Display Effect. 1 Answer

Object menu must be at the center of the object 1 Answer

Destroy on touch for iphone 1 Answer

Run script when object is clicked 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges