Unity Community


Results 1 to 12 of 12

  1. Posts
    19

    yield wait for second problem - does not work

    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:  
    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.     }

    any idea about why it does not work?
    thanks


  2. Posts
    2,603
    try to inverse :

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

  3. Volunteer Moderator
    Posts
    23,726
    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/documenta..._26_Yield.html

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  4. Posts
    2,603
    Code:  
    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. }


  5. Location
    Tennessee, USA
    Posts
    3,465
    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:  
    1. var timeForEvent=5.0;
    2. var nextTime=0.0;
    3.  
    4. function Update(){
    5. if(Time.time> next Time){
    6. doSomeEvent();
    7. nextTime=Time.time + timeForEvent;
    8. }
    9. }

    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. Location
    Bay Area Hobbyist
    Posts
    495
    You dont want to start a new coroutine every clock tic
    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. Volunteer Moderator
    Posts
    23,726
    Quote Originally Posted by BigMisterB View Post
    A Coroutine should only be started if the routine would ordinarily take longer then the update cycle. This should NOT include, moving objects around...
    There's no reason why not, particularly since moving objects over time by definition takes longer than an Update cycle.

    Quote Originally Posted by pakfront View Post
    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.
    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
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  8. Location
    New Jersey
    Posts
    791
    Quote Originally Posted by Eric5h5 View Post
    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
    That's the big reason I use Coroutines. Without them any complicated script becomes spaghetti code pretty quickly.


  9. Posts
    621
    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. Volunteer Moderator
    Posts
    23,726
    The point of coroutines is that you generally don't use Update.

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes


  11. Posts
    621
    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. Volunteer Moderator
    Posts
    23,726
    That doesn't sound like something you would use coroutines for; Update is fine in that case.

    --Eric
    SpriteTile: new tile system that works seamlessly with Unity 4.3 sprites
    FlyingText3D: dynamic 3D text with TTF fonts | Vectrosity: fast & easy line drawing
    Nifty utilities! Stitch terrains together - runtime model importing - file browser - fractal landscapes

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •