Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
1
Question by Adagio-81 · Nov 11, 2018 at 01:45 PM · editortransform.positionincorrect

Transform.position incorrect

The issue is that the transform.position in Inspector doesn't seem to be the same as the position in code

Here's a rundown on what is going on: I have a class called MushroomPosition, which inherits from ItemPositionBase (MonoBehaviour) MushroomPosition doesn't have any code in it (at least not yet), so it is 100% depending on code in ItemPositionBase

I have the following Editor:

 [CustomEditor(typeof(MushroomPosition), true)]
 public class ItemPositionEditor : Editor
 {
     public override void OnInspectorGUI()
     {
         EditorGUI.BeginChangeCheck();
 
         ItemPositionBase item = target as ItemPositionBase;
         SerializedObject targetSerialized = new SerializedObject(target);
 
         item.Row = EditorGUILayout.IntSlider("Row", item.Row, -15, 15);
         item.Column = EditorGUILayout.IntSlider("Column", item.Column, -15, 15);
         item.RowSpace = EditorGUILayout.FloatField("Row space", item.RowSpace);
         item.ColumnSpace = EditorGUILayout.FloatField("column space", item.ColumnSpace);
         item.PositionName = EditorGUILayout.TextField("Position name", item.PositionName);
 
         item.name = item.PositionName + " position:" + item.Row + "," + item.Column;
         
         if (EditorGUI.EndChangeCheck())
         {
             targetSerialized.ApplyModifiedProperties();
             EditorUtility.SetDirty(item);
 
             EditorSceneManager.MarkSceneDirty(EditorSceneManager.GetActiveScene());
         }
     }
 }

In the ItemPositionBase I have the following methods that is being called when Row or Column is changed:

 private void FixRowColumn()
     {
         this.transform.position = GetPositionInformationForItem(Row, Column);
         Debug.Log("X: " + this.transform.position.x);
         Debug.Log("Y: " + this.transform.position.y);
         Debug.Log("Z: " + this.transform.position.z);
         Debug.Log("-------------------");
     }
 
     public Vector3 GetPositionInformationForItem(int row, int column)
     {
         Vector3 result = new Vector3((column * columnSpace), transform.position.y, (row * rowSpace));
     
         return result;
     }

Basically it will set the transform.Position of the selected MushroomPosition object based on the properties.

In the image below the method FixRowColumn was called, where the almost correct (some float rounding issue?) position values are shown in the debug window. Unfortunately it seems like the actual Position values are WAY off. X position is off by 604,1, while the others are not so crazy, but still a bit off

Position

Anybody who has any ideas on why this.transform.Position is not the same as Transform.Position in the Inspector?

EDIT with answer: I found the answer. To set the position of an object to be correctly for child objects, you need to use this.transform.localPosition instead of this.transform.position

transformposition.png (43.3 kB)
Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

Answer by Adagio-81 · Nov 11, 2018 at 02:05 PM

From the look of it, it seems like the position weirdness comes from the grandparent object. If I set the grandparent object to position 0, 0, 0 my object is now positioned correctly

Why would the the grandparent object change the actual values of my object? I understand that by change the position of the grandparent, the object would also move around (but without changing the position values, as the object would still be in the same position compared to grandparent/parent object). But from my understanding, if I set the position of the object to something it would not change depending on parents/grandparents position By this I mean, why would my objects position be set to it's calculated position - position of grandparent?

EDIT with answer: I found the answer. To set the position of an object to be correctly for child objects, you need to use this.transform.localPosition instead of this.transform.position

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
0

Answer by zivbeh · Aug 25, 2020 at 05:37 PM

I have the same problem in my code but this.transform.localPosition is the same as without can you pls help thank you very much!!!

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 remy5623 · Oct 13, 2020 at 07:25 AM 0
Share

transform.localPosition will get scaled by the scale of all ancestors. So for example if your parent object is scaled at (1, 0.5, 1), the transform.localPosition.y of the child object will be multiplied by 0.5. That's what happened to me.

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.

Follow this Question

Answers Answers and Comments

195 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity editor attempts to open a different project to selected and states its already open 0 Answers

balls when moving 0 Answers

Vector3 is not receiving enough arguments from a another script that gives out transform.position. 1 Answer

[C#] [2D] Not changing positions correctly 1 Answer

Moving a GameObject in an Ellipsoid path 1 Answer


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