Problem With Multiple Objects Calling the Same Coroutine

I have several objects which need to do a check relatively often. However, this check creates quite a large overhead, so I’m trying to increase the performance by spreading the check over multiple frames via a coroutine. The check is a large loop, and after it runs x amount of times it yields, and picks up again the next frame. This works.

The problem is that the methods seems to only run x amount of times TOTAL per frame, and not x amount of times per object. This results in only a few objects getting the data it needs, and not all the objects, as is seems to yield for ALL of them once I hit the limit.

Imagine a baseball game. If a team gets 3 outs, then the players who have already hit move to the front of the line. Next time the team is up to bat, they hit again, and the ones who didn’t hit before don’t get to hit now either.

Any solutions? Keep in mind that I’m pretty inexperienced with coroutines.

Pseudo Code
Script 1:

//this is assigned in the inspector
var script2 : Script2;
 
function Update()
{
/*if(long enough has passed since last check and the coroutine has completed for this object) */
script2.CallCoroutine();
}

Script 2

    function Coroutine()
    {
     
        var checksThisFrame : int = 0;
     
        for(var x : int = 0; x < y; x++)
           {
               //Do Stuff
               if(checksThisFrame > maxCheckPerFrame)
                      {
                         yield;
                      }
               checksThisFrame++;
            }
    }

Anyone? It’s worth noting that the coroutine exists in a script separate to the script calling it. All the objects share the same instance of this script.

Why are you calling the coroutine in Update? Why don’t you check for time inside the coroutine itself, if it is not the time you simply return 0 and wait for the next frame. What is “y” in your code?