The question is answered, right answer was accepted
How to destroy child of GameObject?
Sorry if this is in the wrong place, or if this is structured incorrectly.
I've been having trouble debugging my code and I've narrowed it to one particular error. In short, when I try to destroy a child GameObject it doesn't appear to work. Here's my code:
if (EquipmentSlot.transform.GetChild(0) != null)
{
Debug.Log("Equipped item at remove: " + EquipmentSlot.transform.GetChild(0).gameObject);
Destroy(EquipmentSlot.transform.GetChild(0).gameObject);
Debug.Log("Equipped item after remove: " + EquipmentSlot.transform.GetChild(0).gameObject);
}
EquipmentSlot is the parent GameObject. When the code runs, both Debug.Logs output the same thing: the name of the child. I would expect the second log to output null, or similar. Clearly i'm doing something wrong, unless it's a unity bug. Any help would be indescribably appreciated.
Answer by PizzaPie · Jun 02, 2017 at 04:41 PM
If it gets destoyed then you have nothing to worry about, it s because OnDestroy() on the Object, result of Destroy(), is called after the Update() so it won't happen instantly thus the object is still there when 2nd debug is executed. If you want direct result use DestroyImmediate() instead. Cheers.
Follow this Question
Related Questions
How can i make a prefab character to be a child of an existing game object after purchasing 1 Answer
Destroyng all childs of an object 1 Answer
Save created child object to empty GameObject in GameBuild ( Not Editor ) - Save to XML? 1 Answer
Store Rotation values of a Child in a Vector3 or Quaternion ? 1 Answer
How to Destroy A GameObject when it enters any Triggers 2 Answers