In the editor, if I move one object to another, then it will visually save its position. Its local position will be updated, relative to the new parent. How can I do the same script in raintime?
So you’re asking how to access the local position on the Transform component? Use Transform.localPosition.
No, how to transfer an object from one parent to another, keeping its position relative to the screen / canvas.
In the editor, this is done by drag and drop, but is there a ready-made opportunity for runtime?
Vector3 pos = transform.position;
…then set new parent, then:
transform.position = pos;
Also working:
Transform.SetParent(newParent, true);
the second parameter is for “worldPositionStays”
This does not work
This does not work, tried
pweez show you codez!
If you did funny stuff with non-uniform scales and rotations in your hierarchy, it obviously can’t work. If not, this will work when used correctly.
you guys are misunderstanding, the question is to make this in a UI element, which indeed does not work saving the “transform.position” before or even setting the “worldPositionStays” to true in “SetParent()”
This is the Physics forum area. If you want a UI answer, go ask in the UI forum area.