Search Unity

Question Parent change while maintaining position

Discussion in 'Scripting' started by AkhmedAbasov, Jul 9, 2020.

Thread Status:
Not open for further replies.
  1. AkhmedAbasov

    AkhmedAbasov

    Joined:
    Mar 13, 2014
    Posts:
    163
    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?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,456
  3. AkhmedAbasov

    AkhmedAbasov

    Joined:
    Mar 13, 2014
    Posts:
    163
    No, how to transfer an object from one parent to another, keeping its position relative to the screen / canvas.
     
  4. AkhmedAbasov

    AkhmedAbasov

    Joined:
    Mar 13, 2014
    Posts:
    163
    In the editor, this is done by drag and drop, but is there a ready-made opportunity for runtime?
     
  5. adamgolden

    adamgolden

    Joined:
    Jun 17, 2019
    Posts:
    1,555
    Vector3 pos = transform.position;
    ..then set new parent, then:
    transform.position = pos;
     
  6. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    Edy and adamgolden like this.
  7. AkhmedAbasov

    AkhmedAbasov

    Joined:
    Mar 13, 2014
    Posts:
    163
    This does not work
     
  8. AkhmedAbasov

    AkhmedAbasov

    Joined:
    Mar 13, 2014
    Posts:
    163
  9. tjmaul

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    467
    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.
     
  10. Asfryd

    Asfryd

    Joined:
    Aug 23, 2019
    Posts:
    1
    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()"
     
  11. halley

    halley

    Joined:
    Aug 26, 2013
    Posts:
    2,433
    This is the Physics forum area. If you want a UI answer, go ask in the UI forum area.
     
    arkano22 likes this.
Thread Status:
Not open for further replies.