- Home /
Prefab - Child of GameObject.
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)
I added this:
_tempPrefabPos = _childPrefab.transform.position;
_childPrefab.transform.parent = transform;
_childPrefab.transform.position = _tempPrefabPos;
Debug.Log (_childPrefab.transform.position);
The debug.Log gives me the right position(Whch I gave initially to the prefab when I created it.) but for some reason, when I click on the instantiated prefab, the position is different to the one it Logs.... What's happening?
Thanks for your time and help!!!
Do you know the differences between transform.position and transform.localPosition? You may also want to rename childPrefab to something else, as you're changing the reference from the prefab to this instantiated object, so _tempPrefabPos never gets set to the original prefab, but rather your new instantiated object.
You dont need to add that second part of your code. When you set childPrefabs parent, unity will automatically convert the childPrefab's local position so that it wont change the position. Is there anything else you are doing?
I just want my prefab to keep the position I gave to it when I created it(Not when I Instantiated it...)
Im just doing that when I instantiate it...
Thanks for your time guys
@JChilton Debug.Log(_childPrefab.transform.position)
gives me the correct position on the dbg. but not on the game...