Destroy Child of Specific Parent

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);
    }
 } 

1 Answer

1

There's a more elegant solution to this. In the parent script create a function to remove children. In the mouse down of the child objects you can then call that function on the parent and pass the clicked child as the parameter and delete all children up to and including that one.

Here's the OnMouseDown code for the child.

EDIT: Changed syntax of GetComponent. The only thing you need to watch out for here, is that "ScriptName" is whatever name you have given your Parent objects script. ScriptName is just a placeholder in this example.

function OnMouseDown()
{
    transform.parent.GetComponent(ScriptName).DestroyChildAndSiblings(transform.gameObject);
}

Then in your parent which has a script attached "ScriptName" (or whatever name you've given it).

EDIT: Dropped the var keyword.

function DestroyChildAndSiblings(Child : GameObject)
{
    for (var i = 0; i < transform.childCount; ++i)
    {
        if (transform.GetChild(i).gameObject == Child)
        {
            Destroy(Child);
            break;
        }

        Destroy(transform.GetChild(i).gameObject);
    }
}

I wrote this from head and normally write my scripts in C#, so I'm not sure if I have all the JavaScript things correct. But I hope it helps you at least. :)

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!

–

This 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.

–

I'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.

–

I think I see that now, I'll give it a shot. Thanks again

–

Hi, posting an error on this line: function DestroyChildAndSiblings(var Child : GameObject) Unexpected token: var

–