Actually it's best to use InvokeRepeating where possible. I did a test on 5000 objects, where each one runs this:
Code (csharp):
private var i = 0;
function Start () {
InvokeRepeating("Test", .01, 1.0);
}
function Test () {
i++;
}
That normally takes .03 milliseconds per frame, with spikes of 2.38 ms every second (when all 5000 functions run).
Using this code instead:
Code (csharp):
function Start () {
var i = 0;
while (true) {
i++;
yield WaitForSeconds(1.0);
}
}
also takes .03 ms per frame, but the spikes are 4.03 ms every second. Using Update:
Code (csharp):
private var timer = 0.0;
private var i = 0;
function Update () {
if (Time.time >= timer) {
timer = Time.time + 1.0;
i++;
}
}
takes an average of .93 ms per frame (no noticeable spikes every second, since the overhead overwhelms whatever CPU time the actual code uses, even multiplied by 5000). At about 100fps, that's a total of 93 ms per second, whereas the coroutine is a total of 7.03 ms per second and InvokeRepeating is 5.38 ms per second.