- Home /
Delete children of Instantiated object
Hi,
I was wondering if there is any way to instantiate an object without its children? I am trying to do the following:
I have a scene with a lot of objects which all have the same script. They all have a different mesh however, and each has an unique child. For a certain graphical effect, I want to duplicate the object, and add a graphical effect to the duplicate. This largely works using instantiate, but instantiate also duplicates the child which I do not want.
I tried the following:
GameObject copy = new GameObject();
Vector3 fogposition = gameObject.transform.position;
copy = Instantiate(gameObject, fogposition,
gameObject.transform.rotation, gameObject.transform) as
GameObject;
foreach (Transform child in copy.transform)
{
GameObject.Destroy(child.gameObject);
}
This however does not work. Is there any way around this? I cannot destroy the duplicate child by name/tag, as this will also destroy the original child.
When you instantiate, a prefab - it will instantiate the way it's been designed. After the instantiation though, you can alter it, like remove the childs that you don't want it to have. I would probably create a complex type, attach it to the prefab, and based on the values perform certain operations. I would also avoid instantiating and destroying stuff if there is alot of it because it affects the performance, because each time you instantiate something, the memory is allocated to the heap, which eventually keeps growing until you run out of memory. There are object pools that you can use which will greatly improve the performance. (Object pools re-use objects)
Try this
GameObject copy = Instantiate (prefab, transform.position, transform.rotation)as GameObject;
for (int i = 0; i <copy.transform.childCount; i++)
{
Destroy (copy.transform.GetChild(i).gameObject);
}
If it happens a lot you might better off creating seperate prefabs for each object and child objects while linking them through a dictionary inside a scriptable object probably. Then create a custom Instatiate method with the option (parametere) to instatiate the children too or not.
@kaplica the chances of running out of memory in this senario are equal with just instatiating single objects without the child objects, the memory occupied by the child objects will be released as soon they are destroyed, of course will effect negatively the performance.
Answer by Harinezumi · Apr 23, 2018 at 10:46 AM
What doesn't work? What is the effect that you expect and what happens instead? Aside from some unnecessary things, your code seems fine.
Here is your code slightly refactored:
GameObject copy = Instantiate(gameObject, transform.position, transform.rotation, transform);
foreach (Transform child in copy.transform) {
Destroy(child.gameObject);
}
Answer by AkaBrokenarrow · Apr 24, 2018 at 12:50 PM
I must have made an syntax error; using @Harinezumi's refactored code my solution works as I wanted it to work. My own version gave an error I did not recognize. I unfortunately cannot seem to reproduce it. Thanks everyone for all the input!
Indeed, that is weird, as I only did equivalent changes, but I'm glad it worked.
I converted my comment to an answer, so that it is more visible.
Your answer
Follow this Question
Related Questions
GetChild(0) returns nonexistent object 1 Answer
Destroying childs and Instantiate [C#] 0 Answers
Is "null" the best way to unparent a child? 1 Answer
why is transform.position giving me local location?(c#) 0 Answers
transform.childCount == 0, but a child shows up in the hierarchy. How is this possible? 2 Answers