- Home /
Keep instantiated GameObject's position while parenting it to another GameObject
I want to instantiate a GameObject to a position and then change its parent without changing any of its position. I have two GameObjects, the active points in the following images:
I am instanting a prefab at the active point from the first image (the big cube):
GameObject theTile = Instantiate(thePrefab, spawningZone.transform);
Where spawningZone is the big cube. I then want to parent the instantiated object to the active point from the second image (that is the GameObject the script is applied on):
theTile.transform.parent = transform;
But doing this moves my instantiated object to the active point in the second image, instead of staying where I've instantiated it.
I've tried using
theTile.transform.SetParent(transform, false);
But I get the same result. I've also tried saving the local position of the instantiated object before doing the parenting, doing the parenting and then setting back the local position to the one I've saved, but it still doesn't work.
There's an optional second parameter (worldPositionStays) for SetParent, which you are strangely setting to false
in your example.
theTile.transform.SetParent(transform, true);
I tought setting that parameter to false would do it, but I've tried with it set to false and true, but same thing happened.
Answer by RaduMitroi · Jan 16, 2019 at 12:22 PM
I found out what my problem was. It was another line of code that I was using to spawn these prefabs in a grid-like manner and I was messing with the instantiated object's local position after I was setting the new parent. Putting the line of code for setting the parent after messing with the local position solved the issue.
Answer by Kudorado · Jan 11, 2019 at 02:44 AM
Try setting position
instead of localPosition
. Here is an example:
var pos = thePrefab.transform.position;
GameObject theTile = Instantiate(thePrefab, spawningZone.transform);
theTile.position = pos;
hope it help.
Thank you, but that doesn't do it. The object I'm creating still moves itself to the GameObject I'm parenting it to.
Answer by Robotic_Soul · Jan 15, 2019 at 04:33 AM
You know you can pass the parent in the Instantiate call itself? Check out the scripting manual page [1] on it. I would also reccomend setting the vector3 manually... possibly from some variable which you can access and play with at runtime. Something simple like this:
public float SomePosX=0;
GameObject new_cola = Instantiate(prefabObj, new Vector3(SomePosX, SomePosY, 0), newParent.rotation, newParent);
Also you should be aware of the object's center, because that's where it will spawn if you target an object. It's easy to get confused in unity because there is an option next to the default move/scale tools called pivot/center which can make it seem like the object is offset (possibly because of some other large or offset attached object) it's also next to, and relevant to Global/Local button. Be aware of the objects true center when referencing it's transform.
https://docs.unity3d.com/ScriptReference/Object.Instantiate.html
This didn't work. I tried setting the parent in the Instantiate statement so that I'm setting the world position at the same time, but I get the same result. $$anonymous$$y object does not appear at the position (like new Vector3(SomePosX, SomePosY, 0), from your example but with my position).
This is driving me crazy. If I instantiate an object where I want it and don't parent it and run it like that, it appears in the right place, so I hit pause and drag the object manually to the object I want it parented to, and it works! It doesn't move. The object stays at the same world position. I tried a dozen different ways, including yours, to do it programmatically, and it appears in some strange location (nearby) under the ground. Something this simple shouldn't be so hard. I've created outline shaders, I've written an $$anonymous$$$$anonymous$$ORPG server but I can't get an object to appear at a particular world position but while parented to another.
EDIT: let me add a little more info here. The GameObject I'm parenting this to is a child of some huge tree of twisted gameobjects with all different scales and orientations. But that shouldn't matter if I'm specifying the world position, right? How do you escape from all that complexity and say "I just want to place you HERE" but still keep it parented so that when the main object is deleted, so will this GameObject I'm adding.