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??
If your child object is being referenced by name: Code (CSharp): if (Input.GetMouseButtonUp(0)) { GameObject.Destroy(transform.find("ChildObjectYouWantToKill")); }
You could raycast to mouse position, to see which object gets hit and destroy that. http://docs.unity3d.com/ScriptReference/Physics.Raycast.html Or put script with OnMouseDown() and Destroy(gameObject) on each child object, so they get destroyed when clicked.
Derp. @mgear's response is more accurate. I was posting while distracted (PWD) and didn't read the question properly.
problem is i got lots of objects so putting script on everyobject is kinda tedious. Is there any way it will work with putting only 1 script. Objects are same just sprites are different, but there are lots of objects in it.
How are those objects generated? If they are instances of prefabs then you can simply add the script to the prefabs. o:
I did this but it seems to destroy all objects in scene with script attached when i click on one object. How to destroy the only object that you click on t.
Are you sure you're searching through the object hierarchy of the object you clicked on, then? Posting your code is a good way to get specific help, by the way.