- Home /
Instantiate a prefab inside a hiearcy
I have been looking around for this a bit, but havent been able to find anything. I am trying to instantiate a prefab inside an already existing hieracy in the scene.
Lets say that I want an orderly instantiated city. With old and new houses I want all the old houoses to be instantiated under the Old Gameobject, and all the new houses to be instantiated under the New Gameobject. Is this possible?
instantiate the objects and then alter their transforms parent property
Comments are not really meant to post answers. Use comments for asking for clarification of the question, or to comment answers.
Answer by jashan · Aug 30, 2010 at 08:39 AM
That's very easy but you have to know that the hierarchy is stored in the transforms (that's what I had been struggling with when I first tried doing this).
So, what you do is
- Instantiate your prefab and grab the reference into a variable (e.g. newObject)
- Look for the transform of the game object you want to have as parent (e.g. parentObject)
- Call newObject.transform.parent = parentObject.transform
Relevant documentation in the scripting reference: Transform.parent
Hello, i'm trying to do the same thing but with multiple objects being instantiated simultaneously. i can only seem to get the first object to parent through the script...
any ideas?
Thanks
Your answer
![](https://koobas.hobune.stream/wayback/20220613072319im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to destroy all gameobjects active in hierarchy? 1 Answer
How to add an asset to a script-enabled public game object? 1 Answer
Runtime prefab instantiation issue 1 Answer
Prefab instantiated wrong scale on mobile 0 Answers
Prefab Problem 3 Answers