Measuring the length of Objects

In Unity, how can you measure the length of an object?
To make it more clear: What I mean is how long it is in the x axis. The objects have meshes etc.

That totally depends on the object, and on what precisely you mean by length. If the object has a mesh, and the length you want is how long it stretches along one of the axes, you can get the bounding box of the mesh by either of,


But what if the mesh is a crumbled up string of spaghetti in a relatively small bounding box, and you’d like to know how long that string of spaghetti is? Then the question is a lot harder to answer. Can you elaborate on what you mean by length?

width = GetComponent(MeshFilter).mesh.bounds.extents.x;

try to use

vector3 PositionOfTheHead = ((MeshFilter).mesh.bounds.extents.z * gameobject.transform.localScale.z) + gameobject.transform.position.z);

I agree to @CHPedersen, that in most cases that totally depends on the object, and on what precisely you mean by length. But the most straightforward and generic way is just get box, which encapsulates all the gameObject meshes inside. That’s how this can be archived: (using tree iteration to correctly scale child elements with parent scale)

private static Bounds GetTotalMeshFilterBounds(Transform objectTransform)
        var meshFilter = objectTransform.GetComponent<MeshFilter>();
        var result = meshFilter != null ? meshFilter.mesh.bounds : new Bounds();

        foreach (Transform transform in objectTransform)
            var bounds = GetTotalMeshFilterBounds(transform);
        var scaledMin = result.min;
        result.min = scaledMin;
        var scaledMax = result.max;
        result.max = scaledMax;
        return result;

You can do that by using: