• 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
0
Question by user-3865 (google) · Jul 30, 2010 at 12:16 AM · objectdestroycube

Destroy Object on Mouse Click

hi I need click mouse and destroy object

myscript :

var Cube : Transform ;

function Start() {

if (Input.GetButtonDown ("0")) {

Destroy(Cube);

 }

}

Comment
Add comment
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

5 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Statement · Mar 27, 2011 at 01:06 PM

function OnMouseDown() {
    Destroy(gameObject);
}
Comment
Add comment · 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
0

Answer by Sethhalocat · Oct 12, 2014 at 04:48 AM

I tested this and It's guaranteed to work`use :

var Cube : Transform ;

function OnMouseEnter () {

if (Input.GetKeyDown ("Mouse 0")) {

Destroy(Cube); }

}`

Comment
Add comment · 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
0

Answer by 3dDude · Jul 30, 2010 at 12:21 AM

use :

var Cube : Transform ; function Update () {

if (Input.GetKeyDown ("Mouse 0")) {

Destroy(Cube);

    }

}

Comment
Add comment · Show 2 · 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 3dDude · Jul 30, 2010 at 01:02 AM 0
Share

ok, fixed a problem

avatar image Mike 3 · Jul 30, 2010 at 01:05 AM 1
Share

I'd swap it to Input.Get$$anonymous$$ouseButtonDown(0) just to be on the awesome side ;D

avatar image
0

Answer by Timmyglen2 · Jan 03, 2011 at 05:53 PM

Are you trying to destroy it just when you click, or when the mouse is hovering over it and you click? If you are doing it when the mosue is hovering over it, you would change the function from Update or Start to OnMouseEnter. This makes it only work if the mouse hovers over the cube, and then you could just use 3dDude's code to make it work. If you just use the Update or Start function, than whenever you click it will destroy the cube, no matter where you are.

Also, if you are making an FPS type game, then you would probably want to lock the mouse in the center of the screen as well, so that you can aim properly.

Comment
Add comment · 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 Timmyglen2 · Jan 03, 2011 at 06:03 PM 0
Share

Alternatively, you could also change the function to On$$anonymous$$ouseUp, and it should do the same thing (Get$$anonymous$$ouseButtonDOwn or Get$$anonymous$$eyDown will only do the thing I mentioned above, destroying it no matter where you or the mouse is.)

avatar image
0

Answer by poncho · Jan 03, 2011 at 10:59 PM

well, if the object you try to destroy is a GameObject, you can do it like this

fuction Update()
{
   //this if check for the mouse left click
   if (Input.GetButtonDown ("0")) 
   {
      Raycast ray = Camera.main.ScreenPointToRay(Input.MousePosition);
      Raycasthit hit;
      //this if checks, a detection of hit in an GameObject with the mouse on screen
      if(Physics.Raycast(ray, hit))
      {
         //GameObject.Find("Nameofyourobject") search your gameobject on the hierarchy with the desired name and allows you to use it
         Destroy(GameObject.Find(hit.name));
      }
   }
}

i hope this version of my c# code works well in js, shouldnt be difference between them hope this is the answer you are lookin for =)

Comment
Add comment · Show 3 · 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 Windamere · Sep 11, 2011 at 04:48 PM 0
Share

ok I know this is an old thread, but i am having problems with this and so tired of trying to figure it out. here is my script:

using UnityEngine; using System.Collections;

public class $$anonymous$$ouseClickTest : $$anonymous$$onoBehaviour {

 // Use this for initialization
 void Start () {
     
 }
 
 // Update is called once per frame
 void Update () {
         
 
     if(Input.Get$$anonymous$$ouseButtonDown(0));*//Debug returns possible empty statement*
     
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit hit;
     
             
     
     if(Physics.Raycast(ray, out hit, 10f))
             Debug.DrawRay (ray.origin, hit.point);
     
         Destroy(GameObject.Find(hit.collider.gameObject.name));
     }
 }

}

Now The script does work and it Does destroy GameObject, however it's not waiting for Input from mouse, it starts destroying objects as soon as mouse position passes over. Please help and thanks.

avatar image nielshenriksen · Nov 04, 2011 at 05:53 AM 0
Share

Windamere - its because of your last if. It should be

if(Physics.Raycast(ray, out hit, 10f)) { Debug.DrawRay (ray.origin, hit.point);

When you don't have { then the if only uses the next line of code.

avatar image TheJayyBe · Dec 31, 2012 at 04:46 PM 0
Share

I want to know what is RaycastHit hit and what its receive!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Destroying specific clone object while not destroying others on screen 5 Answers

Destroying One Object Destroys Another? 1 Answer

Object won't instantiate at parents position(solved) 2 Answers

Destroy Object during animation 2 Answers

When an object is destroyed? 3 Answers


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