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
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
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
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!!!
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
![](https://koobas.hobune.stream/wayback/20220612181934im_/https://answers.unity.com/themes/thub/images/avi.jpg)