- Home /
Destroying game object
I have a gameobject called item which has script attached to it if (Input.GetMouseButtonUp(0)) Destroy(this.gameObject);
My Gameobject has several child objects. I want to destroy the child object which gets clicked. But the above code destroys parent object as well as the child object. Anyway to only destroy child object??
Answer by Tehnique · Mar 19, 2015 at 11:53 AM
There are 2 ways to do this:
Move your script to each child object, so they are destroyed individually. Each child will have a copy of the script and it will handle being clicked/destroyed.
Raycast from the camera to the pointer, get the clicked object, check for a tag on it or something (your logic here), and then destroy it if needed. Info here, and here.
Your answer
Follow this Question
Related Questions
Destroy Objects to Load New Scene 3 Answers
[JS] Destroy() just don't work :S 0 Answers
Change Score / On Click Destroy Enemy 1 Answer
Problem deleting object 1 Answer