Vertex colour based on distance from transform center

Hey, I’m trying to set colours for a mesh, based on the distance each vertex is from the center of the transform bounds.

I have a VertexColors Component that I’m using for it, and the result I am getting. It seems to be calculating it wrong. Highest points should be grey, others should be green. Any help is appreciated.

[20543-screenshot.454.png*_|20543]

using UnityEngine;
using System.Collections;

public class VertexColours : MonoBehaviour
{
    public Color GrassColour = new Color(68f, 174f, 117f);
    public Color WaterColour = new Color(25f, 59f, 141f);
    public Color DirtColour = new Color(94f, 81f, 8f);
    public Color RockColour = new Color(165f, 165f, 165f);
    public Color SnowColor = new Color(255f, 255f, 255f);

    public void UpdateColors()
    {
        Mesh mesh = this.GetComponent<MeshFilter>().mesh;
        Color[] colors = new Color[mesh.vertices.Length];
        float[] distances = new float[mesh.vertices.Length];

        for (int i = 0; i < mesh.vertices.Length; i++)
        {
            distances _= Mathf.Abs(Vector3.Distance(this.transform.renderer.bounds.center, mesh.vertices*));*_

}

float max = Mathf.Max(distances);
float min = Mathf.Min(distances);

for (int i = 0; i < mesh.vertices.Length; i++)
{
colors = this.VertexColor(distances*, (max + min) / 2f);*
}

mesh.colors = colors;
}

protected Color VertexColor(float distance, float mid)
{
if (distance > mid)
{
return this.RockColour;
}
return this.GrassColour;
}
}

_*

I suspect the problem only occurs if the object is not positioned at Vector3.zero.

The distance comparison on line 20 below compares mesh.vertices which is in local space, to renderer.bounds.center which is in global space.
distances = Mathf.Abs(Vector3.Distance(this.transform.renderer.bounds.center, mesh.vertices*));*
If you change that line to the following, it should work.
distances = Vector3.Distance(this.renderer.bounds.center - transform.position, mesh.vertices*);*
The major change that I’ve made is that we subtract transform.position from renderer.bounds.center to convert this from world space to local space. That way the distance comparison is between vectors in the same (local) space.
(There are also some other minor differences between the two lines that don’t actually make an impact. Mathf.Abs() is unnecessary, as Vector3.Distance() will always be positive (or 0). You also have access to this.renderer directly in a MonoBehavior, and you don’t need to go through this.transform in order to get the renderer).

Solved. Thanks! Your answer was correct.