Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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:
    162
    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:
    10,623
  3. AkhmedAbasov

    AkhmedAbasov

    Joined:
    Mar 13, 2014
    Posts:
    162
    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:
    162
    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,506
    Vector3 pos = transform.position;
    ..then set new parent, then:
    transform.position = pos;
     
  6. tjmaul

    tjmaul

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

    AkhmedAbasov

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

    AkhmedAbasov

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

    tjmaul

    Joined:
    Aug 29, 2018
    Posts:
    464
    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:
    1,905
    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.