Before updating to Windows 10 you may need to return your Pro license. More information here

WaitForSeconds not working at all C#

I have a class file that zooms the camera for aiming. I also have a zoom animation for my gun. What i want to do is wait for the gun to catch up some before i do the camera zoom. It zooms by the right mouse button down event

However, it just doesnt seem to do anything like the function is not being called. I have looked at numerous examples and i dont see what i am doing wrong?

UPDATE: I got it fixed. I will revise my code to reflect that incase anyone else has problems down the road.

 void Update () {
     IEnumerator fieldOfView(){   // Not IEnumerable
             aim = true;
             ready = false;
             aim = false;
             yield return new WaitForSeconds(2);
             cam.camera.fieldOfView = Mathf.Lerp(cam.camera.fieldOfView, distance, speed * Time.deltaTime);
             cam.camera.fieldOfView = Mathf.Lerp(cam.camera.fieldOfView, original_position, speed * Time.deltaTime);

Every example i have tried has not worked.

more ▼

asked Aug 27, 2012 at 07:08 AM

avatar image

30 34 30 32

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

It seems to actually work perfectly in your code.

Starting a coroutine means "executing another piece of code at the same time that your current piece of code". So you're just "waiting" but this does only pause the coroutine code... not the main "thread".

Even if it doesn't seem to wait 5 seconds it actually does, but not in your main execution thread that's why it seems buggy for you.

I would suggest something using a boolean to control your flow. Something like this:

 void Update () 
   if ( ready == false ) // check if you're no in the 5s delay

     aim = true;
     ready_to_anim = false; // set the boolean to false == your not allowed to do something for 5s
   // rest of your code

 IEnumerator wait() 
    yield return new WaitForSeconds(5);
    ready_to_anim = true; // delay is over now you can do something
more ▼

answered Aug 27, 2012 at 07:28 AM

avatar image

897 54 56 65

I tried your way and it just doesnt zoom now.

Aug 27, 2012 at 04:44 PM icelated

I think what i am going to try to do is throw the ifs statements in a function and then just call that function in update. Then, i could pause that function?

Aug 27, 2012 at 04:49 PM icelated

That did the trick. It works now.

Aug 27, 2012 at 04:52 PM icelated

Early exits can produce cleaner code, but only if it's possible to use them. In this case an early exit doesn't make any sense. It will ignore all other code in Update as long as "ready" is false.

I guess the trigger condition should be something like:

 if(ready && Input.GetMouseButtonDown(1))

I'm not sure what the delay should do since it has no function in the question code. It would help to know what's the desired behaviour.

Aug 27, 2012 at 04:59 PM Bunny83

Bunny - i think that would work however i moved the code inside a function and its working now.

Aug 27, 2012 at 05:02 PM icelated
(comments are locked)
10|3000 characters needed characters left

I did not read the code carefully to trace it and see what in each state will happen but your coroutine is returning IEnumerable instead of IEnumerator and it should be the reason for it to not work.

more ▼

answered Aug 27, 2012 at 07:35 AM

avatar image

10.5k 78 127 171

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Aug 27, 2012 at 07:08 AM

Seen: 2646 times

Last Updated: Aug 27, 2012 at 05:56 PM