Simple LOD script problem (switching mesh at runtime)

I have a simple LOD script and it’s not working. The problem I’m having is: the script is telling the mesh filter to change the mesh to a variable from the Lod script and the mesh filter is not obeying. Well anyway here’s the script:

public var mainCamera : Camera;
public var lowMesh : Mesh;
public var midMesh : Mesh;
public var highMesh : Mesh;
public var goneDistance : int = 500;
public var lod0distance : int = 10;
public var lod1distance : int = 20;
public var lod2distance : int = 30;




function Update () {

	var distance = Vector3.Distance(mainCamera.transform.position, this.transform.position);

	var mesh : Mesh = GetComponent(MeshFilter).sharedMesh;
	

	if (distance > lod1distance)
	{
		print ("Distance is greater than lod 1 distance.");
		if (midMesh)
		{
			mesh = midMesh;
			print ("Switched to Mid Mesh");
		}

	}
	if (distance > lod2distance)
	{
		if (lowMesh)
		{
			mesh = lowMesh;
			print ("Switched to Low Mesh");
		}
	}
	
	if (distance > goneDistance)
	{
		renderer.enabled = false;
	}
	else
	{
		renderer.enabled = true;
	}
}

Here’s what it looks like in the Inspector:

By the way, those variables with the values “head” are not all the same, they just have the same name.

And I am getting the messages, Switched to Mid Mesh and Switched to Low Mesh, etc. But the mesh is staying high poly. Any ideas? Thanks!

Okay, I figured it out!

Instead of doing:

var mesh : Mesh = GetComponent(MeshFilter).sharedMesh;

and then

mesh = lowMesh;

now I am simply doing:

this.GetComponent(MeshFilter).sharedMesh = lowMesh;

And it works!