- Home /
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);
}
}
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); }
}`
Answer by 3dDude · Jul 30, 2010 at 12:21 AM
use :
var Cube : Transform ; function Update () {
if (Input.GetKeyDown ("Mouse 0")) {
Destroy(Cube);
}
}
I'd swap it to Input.Get$$anonymous$$ouseButtonDown(0) just to be on the awesome side ;D
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.
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.)
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 =)
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.
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.
I want to know what is RaycastHit hit and what its receive!
Your answer
Follow this Question
Related Questions
if object is destroyed 2 Answers
Destroy objects by clicking on them 1 Answer
Destroy an object with another one. 1 Answer
Can i destroy this cube? Logic?? 1 Answer
particle effect activation 1 Answer