- Home /
How to remove all children objects & how to instantiate prefab as child to specific object
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).
Answer by chillersanim · Oct 17, 2013 at 10:40 PM
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
Well, that's the idea. I have following structure:
Player
|--Camera
|--Hand
...|--HandPrefab
...|--$$anonymous$$aybe$$anonymous$$oreObjects
I want to remove all children to hand so it'll look like this:
Player
|--Camera
|--Hand
Then I'm getting prefab to instantiate from some global item index (already made) and want to instantiate it as child of "Hand" (name not important). So it'll look like this:
Player
|--Camera
|--Hand
...|--HandPrefab
Then again, etc.
Also, JS belongs to browser and only because there isn't anything better until Dart or CoffeeScript will take off and be natively supported by browsers. Please give C# code.
$$anonymous$$y code is c#
This is not JS, and I think that it looks rather different.
The var keyWord is also used in c#, and is often used.
For what you want, you can just use my code, it should work as is.
Greetings
Chillersanim
I have doubts. Would loop fail? As in either not removing all children or trying to remove non-existent ones and thus causing exception? You know, childCount will be decreasing and causing condition to change...
You're right, thanks. Updated the code, should work now.
When the child is removed from the parent, the child is given a completely different position. This is not the solution's fault but Unity, i should clarify. Is there any fix for this?
Answer by rutter · Oct 18, 2013 at 07:57 PM
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;
Answer by movAX13h · Nov 25, 2014 at 08:02 AM
Destroy all children of a GameObject (container):
while(container.transform.childCount > 0)
{
Destroy(container.transform.GetChild(0).gameObject);
}
Code works 100%. If it does not for you, you probably forgot the ".gameObject" part.
Why would it be infinite? Does childCount
not decrease when the children are destroyed?
I don't understand but this broke my code as well, even after using container.gameobject.transform.childCount for both
Answer by Develoop · Feb 27, 2020 at 06:27 PM
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
}
Answer by pjj_gpu · May 08, 2019 at 01:24 AM
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);
}
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Instantiated GameObject Not Rendering 1 Answer