Coroutine does nothing sometimes.

This is really odd. I have a Coroutine that Tessellates a plane for the purpose of LOD creation. Initially it was a function that worked pretty well on its own but as I added more components to my game I needed it to not hog the CPU. I set it as a Coroutine so that it would calculate a number of vertices and triangles before waiting for the next frame. It works well except every now and again it reaches a level of Tesselation and doesn’t do anything until it is called again at which point it resumes working.

E.g. I have a key that cycles through LODs, it goes from low to high detail. I press it once and it goes to the next level of tessellation I press it again and it works, I press it again and nothing happens, I press it one more time and it goes to the next level of tessellation as if I hadn’t pressed the key previously.

IEnumerator Tessalate (int n){

		for (int i = 0; i < n; i ++) {
		
			int cycle = 0;

			int count = triangles.Count;
			List<TriangleIndices> tempTris = new List<TriangleIndices>();

			for (int u = 0; u < count; u ++){

				cycle ++;

				if (cycle > 50){
					cycle = 0;
					yield return new WaitForEndOfFrame();
				}

				int v = verts.Count;
				TriangleIndices t = triangles;

			Vector3 point1 = verts[t.v1];
			Vector3 point2 = verts[t.v2];
			Vector3 point3 = verts[t.v3];

			Vector3 point1a = MeshTools.getMidPoint(point1,point2);
			Vector3 point2a = MeshTools.getMidPoint(point2,point3);
			Vector3 point3a = MeshTools.getMidPoint(point3,point1);

			point1a = MeshTools.putOnSphere(point1a,seed);
			point2a = MeshTools.putOnSphere(point2a,seed);
			point3a = MeshTools.putOnSphere(point3a,seed);

			TriangleIndices t1 = new TriangleIndices(t.v1,v,v+1);

			verts.Add(point1a);
			verts.Add(point3a);

			v = verts.Count;

			TriangleIndices t2 = new TriangleIndices(t.v2,v,v+1);
			
			verts.Add(point2a);
			verts.Add(point1a);

			v = verts.Count;

			TriangleIndices t3 = new TriangleIndices(t.v3,v,v+1);
			
			verts.Add(point3a);
			verts.Add(point2a);
			
			v = verts.Count;

			TriangleIndices t4 = new TriangleIndices(v,v+1,v+2);

			verts.Add(point1a);
			verts.Add(point2a);
			verts.Add(point3a);

			tempTris.Add(t1);tempTris.Add(t2);tempTris.Add(t3);tempTris.Add(t4);
		}

		triangles = tempTris;

	}

}

Where it fails in the sequence varies if I change how many cycles it must go through before yielding.
Any ideas?

Solved!

Just in case anyone else has this kind of weirdness ensure that no other functions are reliant on your Coroutine otherwise it will execute without any input from the Coroutine. For example.

	void Update(){


		if (Input.GetKeyUp(KeyCode.T)) {

			StartCoroutine(Tessalate(1));
			CompileMesh();
		}
	}

The co-routine uses a temporary list for all new vertices and doesn’t update the master list until it is finished. Funnily enough trying to compile before the Coroutine is finished will just end up using the old master file!

EDIT: sorry this was more an example of what not to do and an explanation why. I have moved CompileMesh() to the end of the Coroutine the correct code looks like this;

void Update(){

		if (Input.GetKeyUp(KeyCode.T) {
			
                        StartCoroutine(Tessalate(1));
			
		}
	}

with the CompileMesh() at the end of the Coroutine.