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);

    }

}

function OnMouseDown() {
    Destroy(gameObject);
}

use :

var Cube : Transform ;
function Update () {

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

Destroy(Cube);

       }

}

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.

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 =)

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

var Cube : Transform ;

function OnMouseEnter () {

if (Input.GetKeyDown (“Mouse 0”)) {

Destroy(Cube);
}

}`