Hi, I'm new to this and have combed the internet and unity boards but can't seem to solve this. I'm instantiating 5 prefabs, each having 1 empty parent and 12 children objects. Want to click to destroy a child and all children underneath on the screen. This script works when there is only 1 prefab on the screen but affects any others when they are present.
The children are numerically named 1,2,3.... Then I find the object name that was clicked on and convert it's name to an integer. Then subtract 1 until 0 is reached. Meanwhile all children underneath (less than) are destroyed. Then I turn the int back into a string and feed it into destroy(). Works but destroys other prefabs also.
Since I have both the parent name and child name it should be easy to destroy the children on only 1 prefab. Any ideas or suggestions?
function OnMouseDown () {
var clickparent = transform.parent.gameObject.name;
var clickname = transform.gameObject.name;
print (clickparent);
print (clickname);
var obj : int = parseInt (clickname);
for(var i : int = obj; i > 0; i--)
{
var nextobj = ""+(i);
var kill = GameObject.Find(nextobj);
this.Destroy(kill);
}
}
Thanks Keith. If I'm not mistaken, this would destroy all children of the parent. In my script I need to only destroy the one that is on OnMouseDown and all underneath (leaving the children above). Think of this as a vertical stack of boxes. You click on one to destroy it and all the one's below are destroyed as well. Thanks!
– gregdeviceThis will only destroy the children of the parent from the first, up to and including the child you clicked on. In your vertical stack of boxes example, the bottom box is 0 and you click on 4. This will destroy children; 0, 1, 2, 3 and 4.
– anon45606706I've updated the answer script to explicitly define the type of Child passed into DestroyChildAndSiblings. Give that a try. I've tested this logic on my side and provided you keep the parents child list in the order your objects are stacked, 0 being the bottom, going up, it should work.
– anon45606706I think I see that now, I'll give it a shot. Thanks again
– gregdeviceHi, posting an error on this line: function DestroyChildAndSiblings(var Child : GameObject) Unexpected token: var
– gregdevice