I’m making for my 3D first-person adventure game, system so you can see what item do you hold (like e.g. tools/blocks in Minecraft). However to do it, I need to know how to remove all child objects to specific game object (“hand” or rather pivot where items are spawning) without actually removing that object and how to instantiate object as child to said game object (removing/instantiating will occur rarely as game doesn’t have any high combat).
By “remove” children, do you mean detaching them from the parent, or removing them from the scene?
The GameObject hierarchy is determined by each transform’s parent property.
Let’s suppose we have two Transform references, child
and root
:
Transform child = ...;
Transform root = ...;
//attach child to root
child.parent = root;
//detach child from root
child.parent = null;
//detach all of root's children
root.DetachChildren();
Or, supposing we want to loop through and Destroy() all of root’s children:
foreach (Transform t in root) {
Destroy(t.gameObject);
}
As far as creating a prefab as a child of an object, it’s a two-step process:
- Call Instantiate() to spawn the prefab (this returns a reference to the spawned object)
- Use that reference to access the instance’s transform and set its parent
Something like this:
GameObject trinketPrefab = ...
GameObject trinket = (GameObject)Instantiate(trinketPrefab);
trinket.transform.parent = root;
The parrent-child concept is made with the transform property of each GameObject.
Instead of removing all childobjects instead of the hand, why don’t you make the items be just the child of the hand and delete all of his child without worying what you delete?
Code for adding as child (objectA to objectB):
objectA.transform.parrent = objectB.transform;
Code for removing all childs from an object:
for (var i = objectB.transform.childCount - 1; i >= 0; i--)
{
// objectA is not the attached GameObject, so you can do all your checks with it.
var objectA = objectB.transform.getChild(i);
objectA.transform.parrent = null;
// Optionally destroy the objectA if not longer needed
}
Greetings
Chillersanim
Destroy all children of a GameObject (container):
while(container.transform.childCount > 0)
{
Destroy(container.transform.GetChild(0).gameObject);
}
Notes/Warnings…
Destroy...
is **NOT** immediate (as intended by Unity),
so if you immediately delete 3 and add 3
afterwards you end up with count = 6
e.g. null,null,null, newObj, newObj, newObj
DestoryImmediate...
changes the order in transform array as you delete ( any language thing )
so can't use foreach( child or for( i=0....
as noted in other answer above, must Destroy in reverse order
int count = transform.childCount;
for (int i = count -1; i>=0; i--) {
GameObject.DestroyImmediate(transform.GetChild(i).gameObject);
}
You can’t destroy all childs in one frame for that reason use the follow code [100% working]:
private IEnumerator IClearItems()
{
for (int i = 0; i < content.transform.childCount; i++)
{
Destroy(content.transform.GetChild(i).gameObject);
}
yield return new WaitUntil(() => content.transform.childCount == 0);
// Code to initialize your new items
}