- Home /
Instantiate a prefab inside a parenting GameObject
I have a script in C# that generates a lot of blocks. It uses the instantiate method. How can I make it so that the newly created blocks are created in a hierarchy inside of an empty GameObject? Currently the blocks (prefabs) all appear in the hierarchy when the world is generated and not only does it look messy and doesn't keep everything organized, but also I am using serialization to save the game and I need to attach the script to the GameObject that will hold all the blocks. How can I go about this preferably in C#? Any help would be much appreciated. And yes, I have searched the internet and this forum for this kind of question before you say it.
Answer by dorpeleg · Jul 02, 2013 at 10:07 AM
Start by instantiating an empty game object.
Then, every time you instantiate a block, do this:
block.transform.parent = emptygameobject.transform;
Very simple to do.
Every block created is named "Grass(Clone)", "Dirt(Clone)", and "Stone(Clone)" so how can I make this script apply itself to each of the clones which don't exist until the generation script runs?
When instantiating, do this:
GameObject temp = [instantiate code here];
temp.transform.parent = emptygameobject.transform;
That's all.
Your answer
Follow this Question
Related Questions
How do you destroy more than one prefab clone? 1 Answer
Decals? How Do I add them to walls? 1 Answer
How to climb Ledges? 0 Answers
How to destroy a gameobject when I collide with it? 2 Answers