0
\$\begingroup\$

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!");
        Thread.Sleep(100);
    }
}

The error code is this:

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

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.

\$\endgroup\$
4
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – DMGregory Feb 18 at 1:53
  • \$\begingroup\$ @DMGregory I know, that’s why I said your answer is better. But, I was just trying to help. \$\endgroup\$ – OKprogrammer Feb 18 at 2:23
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – D.Kallan Feb 18 at 7:40
  • \$\begingroup\$ @D.Kallan I know. I want to remove it, because DMGregory’s answer is much better. \$\endgroup\$ – OKprogrammer Feb 18 at 13:51
6
\$\begingroup\$

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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.