- Home /
prefab not being instanced if its added as a child of another prefab,Why adding transform to instanciate breaks the code?
I have this code executed in a prefab called Wheel. This prefab has to instanciate another prefab called slice and add it as a child.
This works: GameObject slice = Instantiate<GameObject>(sliceToCreate);
It properly instanciates the slice, its added to the hierarchy and shown in the scene BUT its not added as a children. To add it , as far as a I know I can add transform as a second parameter.
GameObject slice = Instantiate<GameObject>(sliceToCreate, transform);
But with this line, the objects are not instanciated, or at least, ar not shown in the hierarchy and scene.
Even if I try slice.transform.parent = transform;
after it, it breaks. ("setting the parent of a transform which resides in a prefab is disabled to prevent data corruption")
Aside from what you asking, you can have prefab that has a child game object(s). Chain instantiating might cause other unseen problems.
For instantiating, see docs if you didn't already:
https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
And see this Q&A that might related with your problem.
https://answers.unity.com/questions/341714/setting-the-parent-of-a-transform-which-resides-in.html
From above link, "attempting to parent a prefab" is not allowed. try:
GameObject slice = Instantiate<GameObject>(sliceToCreate);
slice.transform.parent = transform;
Your answer
Follow this Question
Related Questions
Creating a weapon database from prefabs 1 Answer
Hiding gameobjects in hierarchy make it less expensive ? 0 Answers
Changes done in prefab script get applied to all instances of that prefab. 0 Answers
Creating a scene: instantiating a prefab in a scene or use pre-instantiated prefabs? 1 Answer
Link GUIText to a prefab? 2 Answers