- Home /
Click To Delete Specific Object
I'm making a game where you go around collecting notes off of trees (much like Slender, but simpler, and with only trees) to give my friends a good scare and to help learn Unity. Right now, I'm having trouble getting the notes to disappear once clicked at a close range. I have this code attached to my object, Note1, and my first person camera (because I'm not sure which one the code for object deletion should go on). Right now, the code deletes anything that I click on (the tree, terrain, etc). Help please?
function Update ()
{
if ( Input.GetMouseButtonDown(0) )
{
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, hit, 5.0))
{
Destroy(hit.collider.gameObject);
}
}
}
Answer by aerhx · Apr 15, 2013 at 02:22 AM
I know this is an old question, but here's the answer. I'm doing a similar thing and was having the same issue. I recommend setting the tag to "page" or something and then doing this:
if(hit.collider.tag == "page"){
Destroy(hit.collider.gameObject);
}
Your answer
Follow this Question
Related Questions
finding and destroying specific children 1 Answer
Selecting gameobjects in game, 3 Answers
Destroying specific clone object while not destroying others on screen 5 Answers
On Trigger Delete Object 1 Answer