- Home /
Instantiating Child - Prefabs
Hello everyone.
Quick question. I've created a sprite prefab.
But I've created the prefab in ParentGameObject.
How do I Instantiate the prefab as a child of this(this being ParentGameObject), and keep its own position? I instantiate as the following:
childPrefab = Instantiate(Resources.Load(childPrefab)) as GameObject;
childPrefab.transform.parent = this.transform;
You see, the position of the prefab changes, when I added one when I created the prefab. Is it possible to keep the positioning of the original Prefab I created (being a Child of course) without hardcoding? (There's a lot of prefabs Im creating)
Thanks for your time and help!!!
Answer by spraw · Jun 30, 2014 at 07:20 PM
I'm not entirely sure if I fully understood your question. But could you possibly try to instantiate the object, and cache it's position right there. Now you assign it as a child to a parent and adjust its position with the cached/saved position?
That way you don't have to hardcode anything. Hope I got it correctly. :)
Thanks for your time, but how would I do that?
You see, I anchored the childPrefab to the ParentGameObject because the parent is the player and the child is a "shirt". as the shirt is a sprite, I made it so it fits the player best. The idea is that I click a button to take it off or put the shirt on.
But when the shirt is instantiated it isn't in the right place, despite the fact that I saved the prefab so It fit perfectly. So Im asu$$anonymous$$g when I give it the parent.transform it literally takes the same coords despite the coords I originally gave the prefab...
Hey, can you give me these values for the shirt:
1: When in prefab, position values before you instantiated
2: When instantiated, position values before parented
3: When instantiated, position values after parented
This way we know exactly where the values become corrupt and can go from there
Your answer
![](https://koobas.hobune.stream/wayback/20220613152901im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Why is my instantiated prefab invisible ? 2 Answers
How to generate different diagonal platforms? 0 Answers
2D Projectile Not Firing Based on Rotation 1 Answer
Prefab - Child of GameObject. 0 Answers