How to add time delay in Unity C#

So I'm trying to make a camera zoom in, but Thread.Sleep doesn't work. Here is the code:

    void Start() {
for (int a = 0; a > 1000; a = a + 1)
{
Debug.Log("Done!");
}
}


The error code is this:

Assets\StartScene\CamScript.cs(12,13): error CS0103: The name 'Thread' does not exist in the current context


You are getting this error message because you are missing a line that accesses the API for this. Simply add using System.Threading; at the top of your script. This will access the API for it. Or use a coroutine, which won’t freeze the entire game as a whole and is a much better alternative, as DMGregory suggested.

• using System.Threading; will remove the compiler error, but replace it with a stall, where the game as a whole freezes for a tenth of a second. Not exactly ideal in this context. – DMGregory Feb 18 at 1:53
• @DMGregory I know, that’s why I said your answer is better. But, I was just trying to help. – OKprogrammer Feb 18 at 2:23
• Sorry but I dont think this should be the answer (it's the OP's choice, but) seeing as it would make people think they can use T.Sleep to add delays. And I just see the same answer as DMGregory's but much shorter (the word coroutine). You dont really say his answer is better.. nor that T.Sleep shouldnt be used. Your answer reads as: "better than Sleep, use a coroutine" which has a different meaning. – D.Kallan Feb 18 at 7:40
• @D.Kallan I know. I want to remove it, because DMGregory’s answer is much better. – OKprogrammer Feb 18 at 13:51

You want a Coroutine.

// IEnumerator tells Unity to run this as a coroutine
// that can suspend and wait, without stalling the whole game.
IEnumerator Start() {
// Your loop condition was backwards,
// so it would never execute the body.
// It looks like you want a < here instead.
for (int a = 0; a < 1000; a++)
{
Debug.Log("Done!");
// yield return means "suspend this method"
// and the WaitForSeconds tells us when to resume it.
yield return new WaitForSeconds(0.1f);
}
}


An important thing to understand about Unity is that the MonoBehaviour message methods like Start() and Update() are called on the main thread (ie. single-threaded). So if you stall one of them by hitting a long-running loop or sleeping the thread, you cause the entire game to hang.

So, if you want to add a delay, it needs to be by yielding control back to the engine temporarily - so it can continue running other update functions, accepting input, rendering frames, etc. - until you are ready to resume your work.