• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by leonardoraele · Apr 20, 2014 at 06:02 PM · transformpositionparenthierarchy

How to change game object's parent in hierarchy without changing it's transform's position?

When we move a game object to be child of another game object in hierarchy, Unity changes it's transform position so that the game object can maintain it's global position in the world.

I think it's a pain to always reset and/or recalculate positions when moving a game object in hierarchy so I want to know if there's a simple/easier way to do this: move the game object so that it can maintain it's LOCAL position. As if we were just writing transform.parent = parentGameObject.transform via scripts.

For example:

  • EmptyObjectP1 position is (0, 0, 0)

  • EmptyObjectP2 position is (10, 0, 0)

  • EmptyObjectChild is currently child of EmptyObjectP1 and it's position is (2, 0, 0)

  • Now, if we move EmptyObjectChild to become parent of EmptyObjectP2, it's position will change to (-8, 0, 0) and it will stay in the same position (2, 0, 0) in the world. I want it to stay (2, 0, 0) so that it's global position change to (12, 0, 0)

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Apr 20, 2014 at 06:09 PM 2
Share
  • get the localPosition of the child

  • store value in temporary variable

  • change the parent

  • set the localPosition to the stored variable

avatar image leonardoraele · Apr 20, 2014 at 07:07 PM 0
Share

Where in the editor can I write a code for this?

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by AlucardJay · Apr 21, 2014 at 02:00 AM

Here are a couple of scripts to add this functionality in Edit mode. In the Editor folder, add the script ObjectMoverInspector. You can place the script ObjectMover anywhere (in a folder called Helpers for example). Yes, I have awesome naming conventions =]

If the new menu item doesn't appear, just reopen the project. Then you can select Custom > Object Mover. This creates an empty object in the scene with the script ObjectMover attached. Drag and drop your child and new parent object in the Inspector then click the button.

ObjectMoverInspector.js

 #pragma strict
 
 @CustomEditor( ObjectMover )
 
 
 class ObjectMoverInspector extends Editor 
 {
     
     var childToMove : SerializedProperty;
     var newParent : SerializedProperty;
     
     var obj : GameObject;
     var objScript : ObjectMover;
     
     
     function OnEnable() 
     {
         obj = Selection.activeGameObject;
         objScript = obj.GetComponent( ObjectMover );
         
         childToMove = serializedObject.FindProperty( "childToMove" );
         newParent = serializedObject.FindProperty( "newParent" );
     }
     
     
     function OnInspectorGUI() 
     {
         // Update the serializedProperty - always do this in the beginning of OnInspectorGUI.
         serializedObject.Update();
         
         
         // childToMove
         EditorGUILayout.BeginHorizontal();
         EditorGUILayout.PropertyField( childToMove );
         EditorGUILayout.EndHorizontal();
         
         // newParent
         EditorGUILayout.BeginHorizontal();
         EditorGUILayout.PropertyField( newParent );
         EditorGUILayout.EndHorizontal();
         
         
         // Editor GUI Buttons
         EditorGUILayout.BeginHorizontal();
         
         if ( GUILayout.Button( "Reparent and position", GUILayout.MinWidth( 80 ), GUILayout.MaxWidth( 250 ) ) )
         {
             objScript.ChangeParentAndReposition();
         }
         
         EditorGUILayout.EndHorizontal();
         
         
         // Apply changes to the serializedProperty - always do this in the end of OnInspectorGUI.
         serializedObject.ApplyModifiedProperties();
     }
 }
 
 
 @MenuItem( "Custom/Object Mover" )
 static function CustomObjectMover() 
 {
     var go : GameObject = new GameObject( "_ObjectMover" );
     go.transform.position = Vector3.zero;
     go.transform.rotation = Quaternion.identity;
     go.AddComponent( ObjectMover );
 }


ObjectMover.js

 #pragma strict
 
 public var childToMove : Transform;
 public var newParent : Transform;
 
 private var localPos : Vector3;
 private var localRot : Quaternion;
 
 
 function ChangeParentAndReposition() 
 {
     if ( !childToMove || !newParent )
     {
         Debug.LogWarning( "Assign Objects in Inspector" );
         return;
     }
     
     Debug.Log( "Change Parent And Reposition" );
     
     localPos = childToMove.localPosition;
     localRot = childToMove.localRotation;
     
     childToMove.parent = newParent;
     childToMove.localPosition = localPos;
     childToMove.localRotation = localRot;
 }

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · May 25, 2014 at 04:11 PM 0
Share

How did you go?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Updating localPosition(?) in a scaled transform parent 1 Answer

Problem with making child an object 2 Answers

Object with many children will not show up in the center although its coordinates are set to 0, 0, 0 1 Answer

Position of parent-Object 2 Answers

Setting parent in instantiated class 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges