Destroyng all childs of an object
What i want to happen is that i destroy all child gameobjects of a parent from a script that is not attached to the parent gameobject. the problem is that it doesn't search the parent gameobject and that it just tries to delete his own gameobject.
clearing things out i have an gameobject named "selector".
to the selector there is a script attached (C#) named "itemSelector".
i have a second gameobject named "WeaponHolder".
what happens is i try to acces the childeren of "WeaponHolder" from the "itemSelector" script but its just start searching the childeren of "selector"
Code:
DestroyObject(GameObject.Find("WeaponHolder").transform.GetChild(0));
if have tried different version like
DestroyObject(GameObject.Find("WeaponHolder").transform.GetChild(0).gameobject);
Destroy(GameObject.Find("WeaponHolder").transform.GetChild(0));
Destroy(GameObject.Find("WeaponHolder").transform.GetChild(0).gameobject);
public GameObject destroy
destroy = (GameObject.Find("WeaponHolder").transform.GetChild(0));
Destroy(destroy)
Answer by AngryKiwiArts · Nov 03, 2016 at 11:45 AM
public GameObject WeaponHolder;
foreach (Transform child in WeaponHolder.transform) { GameObject.Destroy(child.gameObject); }
?