Get position of a child object

Ok, I have been pounding my head against the wall and can’t seem to find the position of a child object.

I have a model named ‘shapegroup’.
That model has children named
myshapes_myCircle
myshapes_myCone
myshapes_mySquare.

I have shapegroup positioned at 0,0,0.
When I check the positions of each of the children… they all come back the same as the parent - which as you can see from the image below… all of them are in different positions

Here is the code I am running:

public class getPositions : MonoBehaviour {

    public GameObject shapegroup;
    // Use this for initialization
    void Start () {
        shapegroup = GameObject.Find ("shapegroup");
        reportTransform (shapegroup.transform.position, shapegroup.name);
        foreach (Component c in shapegroup.GetComponentInChildren<Transform>()) {
            reportTransform(c.transform.position, c.name);
                }
    }

    void reportTransform(Vector3 trans, string transName){
        Debug.Log ("This is "+transName+ " X:"+trans.x.ToString()+" Y:"+trans.y.ToString()+" Z:"+trans.y.ToString());
    }
}

And here are the results…

How do I get the world coordinates of the child objects?

My guess is those are custom meshes you created, where the positions of the meshes are off-origin. The actual world transform coordinates are exactly what Unity is telling you they are, though.

–Eric

Hi Eric,

Thanks for the reply. I have updated my code and much like you alluded to, the objects are located at the same position of the parent (0,0,0) with the meshes transformed.

So if anyone else runs into this issue, this is what I did to get the coordinates of the children meshes.

    public GameObject shapegroup;
    // Use this for initialization
    void Start () {
        shapegroup = GameObject.Find ("shapegroup");
        reportTransform (shapegroup.transform.TransformPoint(Vector3.zero), shapegroup.name);
        foreach (Transform c in shapegroup.GetComponentInChildren<Transform>()) {
            GameObject go=c.gameObject;
            //we need the location of the mesh - not the object
            MeshFilter myMesh=go.GetComponent<MeshFilter>();
            Mesh m=test.mesh;
            //Bounds has mesh placement informaton
            Bounds b= m.bounds;
            reportTransform(b.center, go.name);
                }
    }

    void reportTransform(Vector3 trans, string transName){
        Debug.Log ("This is "+transName+ " X:"+trans.x.ToString()+" Y:"+trans.y.ToString()+" Z:"+trans.y.ToString());
    }

}

BOOM!!!

Thanks again Eric and have a great day!