Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

yield wait for second problem - does not work

Discussion in 'Scripting' started by levye, Mar 10, 2011.

  1. levye

    levye

    Joined:
    Dec 15, 2010
    Posts:
    19
    Hi,

    I have a problem with yield return new WaitForSeconds(2)
    The problem is i have a function called in FixedUpdate and i want that function to be wait 2 seconds on its first run but it does not work,the code is shown below,


    Code (csharp):
    1. void FixedUpdate () {
    2.        
    3.     if(start_cam_animation)
    4.     {
    5.         SmoothCameraAnimation(Camera.main.transform.position, camDesiredPos,Camera.main.orthographicSize,zoomOutSize);
    6.             if(Camera.main.transform.position == camDesiredPos || Camera.main.orthographicSize == zoomOutSize){
    7.                 start_cam_animation = false;
    8.             }
    9.            
    10.         }
    11.  
    12. .......
    13. ......
    14.  
    15.     void  SmoothCameraAnimation(Vector3 fromPos, Vector3 toPos, float fromSize, float toSize)
    16.     {  
    17.         if(first_run){
    18.              StartCoroutine(waitForSeconds());
    19.                 first_run = false;
    20.            
    21.         }..
    22.  
    23. ..
    24. ...
    25. ...
    26.  
    27. and finaly
    28.  
    29.     private IEnumerator waitForSeconds(){
    30.        
    31.                 yield return new WaitForSeconds(2);
    32.        
    33.     }
    34.  
    any idea about why it does not work?
    thanks
     
  2. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    try to inverse :

    Code (csharp):
    1. if(first_run){
    2.                 first_run = false;
    3.              StartCoroutine(waitForSeconds());
     
  3. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Putting "yield return new WaitForSeconds(2);" into a function which does nothing else won't accomplish anything; you might as well use WaitForSeconds directly in the appropriate place. Besides which, you're just starting that coroutine and not yielding on it, so although it's running, that won't have any visible effect. Also, the "start_cam_animation" check in FixedUpdate isn't really efficient; you should call the coroutine directly (also FixedUpdate is for physics and isn't a good place for anything else). You may want to read the docs again if you haven't already: http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html

    --Eric
     
  4. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class NewBehaviourScript : MonoBehaviour {
    5.  
    6.     bool firtsTime = true;
    7.  
    8.     void FixedUpdate () {
    9.         if(firtsTime) {
    10.             firtsTime = false;
    11.             StartCoroutine("test");
    12.             StartCoroutine("test2", 5.0F);
    13.         }
    14.     }
    15.    
    16.     IEnumerator test () {
    17.         yield return new WaitForSeconds(3);
    18.         Debug.Log("test");
    19.     }
    20.    
    21.     IEnumerator test2 (float someParameter) {
    22.         yield return new WaitForSeconds(someParameter);
    23.         Debug.Log("test2 ");
    24.     }
    25. }
    26.  
     
  5. bigmisterb

    bigmisterb

    Joined:
    Nov 6, 2010
    Posts:
    4,221
    Nooooo......

    You dont want to start a new coroutine every clock tick... your game would crash real quick. Honestly, your best bet is to put a timer system into your Update, not Fixed Update...

    Code (csharp):
    1.  
    2. var timeForEvent=5.0;
    3. var nextTime=0.0;
    4.  
    5. function Update(){
    6. if(Time.time> next Time){
    7. doSomeEvent();
    8. nextTime=Time.time + timeForEvent;
    9. }
    10. }
    11.  
    This way your event happens every X seconds and does whatever it is you need.

    A Coroutine should only be started if the routine would ordinarily take longer then the update cycle. This should NOT include, moving objects around...

    A typical Coroutine would move large amounts of data within a timeframe. So say you wanted to load a texture in the background. That would be a Coroutine.
     
  6. pakfront

    pakfront

    Joined:
    Oct 6, 2010
    Posts:
    551
    He doesn't, only on the 'firts' tick. Does your argument against using a coroutine as a timer still apply in this case? Just wondering 'cause there seem to be two camps of thought on this forum. Not sure if I'm Team Coroutine or Team Update.

    yes, I realize they are not mutually exclusive, but there does seem to be a train of thought that emphasises doing every timestepping or time-intervaled thing as a coroutine and avoiding Update due to it's supposed cost.
     
  7. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    There's no reason why not, particularly since moving objects over time by definition takes longer than an Update cycle.

    It's really more about making the code simpler and less bug-prone. Instead of a mess of various nested if/then conditionals inside Update that tries to make things happen or stop happening at given times, a few coroutines would typically be easier to use.

    --Eric
     
  8. KyleStaves

    KyleStaves

    Joined:
    Nov 4, 2009
    Posts:
    821
    That's the big reason I use Coroutines. Without them any complicated script becomes spaghetti code pretty quickly.
     
  9. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    i would like to start that practice of using coroutines more, can you give me an example of how to use it from update for example?
     
  10. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    The point of coroutines is that you generally don't use Update.

    --Eric
     
  11. pretender

    pretender

    Joined:
    Mar 6, 2010
    Posts:
    862
    so for example, i am checking for mouse movement that controls rotation of the camera in update. how to avoid that? and use it from coroutines?
     
  12. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That doesn't sound like something you would use coroutines for; Update is fine in that case.

    --Eric