- Home /
Destroy child from parent in array (Child instatiate)
HI, how i can destroy a child that was instatiate, from a parent in a array ?
this is a error:
Destroy(actualParent.transform.FindChild("actualChild(Clone)").gameObject);
the error is: NullReferenceException: Object reference not set to an instance of an object
and by this nothing happend:
Destroy(actualParent.transform.FindChild("actualChild(Clone)"));
or is is better to disable it ?
Cloned objects don't have the same parent as the original. Did you reassign the clone to have the same parent? You also might want to just store its reference with a variable when creating it.
Answer by Piflik · May 03, 2016 at 12:51 PM
This should work if there is an object parented to 'actualParent', which is named 'actualChild(Clone)' (make sure the name doesn't have a space before the opening parenthesis). If not, it at least doesn't throw an exception.
GameObject go = actualParent.transform.FindChild("actualChild(Clone)");
if(go != null) Destroy(go.gameObject);
If possible, I would rather store a reference to the object when it is created, to avoid searching through the hierarchy and comparing strings is usually not the most robust way of identifying objects.
Your answer
![](https://koobas.hobune.stream/wayback/20220612080053im_/https://answers.unity.com/themes/thub/images/avi.jpg)