# Unity Transform.Position not returning expected position

So in my scene hierarchy, I have an object laid out like so; -Level1 --Level2 ---Level3 ----Level4

My script is attached to the Level1 object. When I try to find the transform.position of Level4 by way of Debug.Log to the console, I get the position of Level1, and so when I change the position of Level4 inside Level3, the data sent to the console doesn't change, still shows the position of Level1.

I do not believe this may be relevant, but what I am doing is an IK rig to move a character's limb. The object for which I am attempting to find the position is nested inside the skeleton of the character. The value returned to me is the position of the prefab placed in the scene. All of the objects (Level1 through Level4) are not explicitly moved, however they inherit their positions from the rotation of their parent.

My code to read the position is as follows:

Debug.Log(object.position);


Where object is already a transform. I have also tried:

Debug.Log(object.transform.position);


But still, all I get is the position of the top level parent.

Thanks for any help.

Ok so to expand on the question, I'll explain exactly what I'm trying to do. I have the mecanim example and unity 5.0.1f1. I have the Mecanim Example project from the asset store and I'm in the animator controller scene. I have enabled IK pass on my base layer, and have placed my code in the "idle run jump" script, attached to the top level of the game object for the playable character. I have then attached an empty game object inside the left foot to mark the location of where the left heel meets the floor. This object is attached in the inspector to a variable in the "idle run jump" script, which then receives the ik pass through the function OnAnimatorIK. My aim is to get the foot to follow the same position as another object, where leftFoot follows leftFootObj. My code is as follows;

using UnityEngine;
using System.Collections;

public class pos : MonoBehaviour {

protected Animator animator;
public float offset = 0.156f;

public Transform leftFoot;
public Transform leftFootObj;

void OnAnimatorIK() {
if (animator == null)
return;

if (leftFoot != null) {
Debug.Log(leftFoot.position);
animator.SetIKPositionWeight(AvatarIKGoal.LeftFoot,1);
animator.SetIKRotationWeight(AvatarIKGoal.LeftFoot,1);
animator.SetIKPosition(AvatarIKGoal.LeftFoot,(leftFootObj.position+(Vector3.up*offset)));
animator.SetIKRotation(AvatarIKGoal.LeftFoot,leftFootObj.rotation);
}
}

void Start ()
{
animator = GetComponent<Animator>();

if(animator.layerCount >= 2)
animator.SetLayerWeight(1, 1);
}

}

• Your question is a bit ambiguous, Debug.Log(object.position); is attached to the nested Lvl4 object I presume? did you try Debug.Log(this.transform.position); – Zee Jun 18 '15 at 11:25
• Thanks for the reply. The script in question is attached to the level1 object, and so I have created a script into the level4 object, and done a Debug.Log of it's position, which returns the exact same information. I am wondering whether there may be a problem due to the fact that the position of the level 4 object is affected by the rotation of the level2 and level3 objects? – Michael Thompson Jun 18 '15 at 11:58
• Add the exact code you are using, without it your question will be almost impossible to troubleshoot. – Jeremiah Leslie Jun 18 '15 at 15:49
• Sorry, forgot to comment. I added the information you requested. – Michael Thompson Jun 19 '15 at 6:39
• Bump. Don't suppose anyone's got any more suggestions? – Michael Thompson Jun 21 '15 at 13:28

The position of level4 is dependent of all its parents, if you want to attach your script to the parent level1, you can call the first child of a gameObject by :

this.transform.GetChild(0).transform.GetChid(0);


This will address the first child of the child of the gameObject attached to the script, if you want to go deeper, just repeat the function. If you would like to call the child directly by Name instead of using this method, just use :

GameObject.Find(Level1/level2/level3/level4);


and then address its transform, or whatever you need from it.

• I've already targeted my object, which I have placed at the top of my script to show in the inspector. I have verified I have selected the correct game object. I already have the transform I need to address, however when I look at the position in my script, it gives me an unexpected position value, regardless of where the level 4 game object actually is in the 3d world. – Michael Thompson Jun 18 '15 at 12:11
• Remember, the level4 object's position is relative to his parent, if the parent is in xy(1,1) and the child is in xy(1,1) coordinates, then the childs coordinates are xy(2,2) in the world. – Zee Jun 18 '15 at 19:24
• Yes I'm aware of that but that's not the problem. The object is attached to a limb on a character. When requesting the position of the limb (not local position), it returns the value of the parent's position which is different to the position of the object for which I wish to request the position – Michael Thompson Jun 19 '15 at 2:22

Despite my late reply this answer might be the solution that some people are looking for. It might save you hours of debugging as it would've saved me quite a lot if I found an answer like this. The answer is bad pivot points.

I had the same issue where the nested childs of a gameObject had the same position as the top level parent. In Unity the pivot point is default shown at the center of the gameObject. However this might not be the case in the actual model! Simply by toggling the tool handle position from Center to Pivot should show if pivot points are the issue here (which it was in my case).

The artist on my end had simply detached objects to create children, however the pivot point was not reset to be a zero position of the new mesh (child). It was still the same point as that of the previous mesh (parent). This resulted in the pivot points of nested children being the same as the parent.

If you encounter the same problem, triple check the pivot points in your modelling software before starting countless hours of debugging trying to figure out what is resulting the "fake" position... while the pivot simply isn't in expected position.

• Note that you can toggle where Unity displays the gizmo of an object, between the center of the object and its pivot (local 0,0,0), so it's not "always" shown at the center. See the Gizmo Toggles described here – DMGregory Aug 1 '18 at 2:20
• I wasn't aware of this, thanks for the heads up! I have edited my answer. – ploopploop Aug 1 '18 at 2:27

I caused this issue to myself by organizing my game objects within an empty game object that didn't have a transform x/y/z of zero. Set that transform x/y/z to zero, and I started getting the correct numbers again.