Search Unity

getting WaitForSeconds to work?

Discussion in 'Scripting' started by stiltskin, Feb 27, 2010.

  1. stiltskin

    stiltskin

    Joined:
    Feb 27, 2010
    Posts:
    95
    can someone tell me why the WaitForSeconds does not work in this script? It seems to get ignored.

    Code (csharp):
    1.  
    2. public float FlickerSpeed = 0.1f;
    3.  
    4. IEnumerator SlowUpdate(){
    5.   yield return new WaitForSeconds (
    6. }
    7.  
    8. void Start () {
    9. }
    10.    
    11. void Update () {
    12.   // If flicker set to Intesity OR Both
    13.   if( flickerMethod == flickerMethods.Intensity || flickerMethod == flickerMethods.Both )
    14.   {
    15.     light.intensity = Random.Range( IntensityMin, IntensityMax );
    16.   }
    17.   // If flicker set to Range OR Both
    18.   if( flickerMethod == flickerMethods.Range || flickerMethod == flickerMethods.Both )
    19.   {
    20.     light.range = Random.Range( RangeMin, RangeMax );
    21.   }
    22.  
    23.   SlowUpdate();
    24. }
    Thank you!
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It's running fine, but since you aren't doing anything other than yielding in the SlowUpdate function, you won't see any results. It's not going to slow down Update or anything. Update runs every single frame regardless, so if you don't want this behavior, don't use Update.

    --Eric
     
  3. stiltskin

    stiltskin

    Joined:
    Feb 27, 2010
    Posts:
    95
    thank you for the quick reply!

    Sadly im not very good at this stuff. do you have a suggestion of how to do this so that the script is constantly running?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I think you want this (untested):

    Code (csharp):
    1. public float FlickerSpeed = 0.1f;
    2.  
    3. IEnumerator Start () {
    4.     while (true) {
    5.         // If flicker set to Intesity OR Both
    6.         if( flickerMethod == flickerMethods.Intensity || flickerMethod == flickerMethods.Both )
    7.         {
    8.           light.intensity = Random.Range( IntensityMin, IntensityMax );
    9.         }
    10.         // If flicker set to Range OR Both
    11.         if( flickerMethod == flickerMethods.Range || flickerMethod == flickerMethods.Both )
    12.         {
    13.           light.range = Random.Range( RangeMin, RangeMax );
    14.         }
    15.        
    16.         yield return new WaitForSeconds (FlickerSpeed);
    17.     }
    18. }
     
  5. stiltskin

    stiltskin

    Joined:
    Feb 27, 2010
    Posts:
    95
    it doesnt seem to be working. Getting tons of errors. ill post the rest of the script, something else im doing is causing me headaches...

    This is what I changed it to after your post.

    really sorry for these noob questions!

    Code (csharp):
    1. public enum flickerMethods { Intensity = 0, Range = 1, Both = 2 };
    2. //public flickerMethods flickerMethod = flickerMethods.Intensity;
    3.  
    4. // MinIntensity
    5. public float IntensityMin = 0.5f;
    6.  
    7. // MaxIntensity
    8. public float IntensityMax = 0.1f;
    9.  
    10. // MinRange
    11. public float RangeMin = 10.0f;
    12.  
    13. // MaxRange
    14. public float RangeMax = 2.0f;
    15.  
    16. //delay for flickering speed
    17. public float FlickerSpeed = 0.1f;
    18.  
    19. IEnumerator Start () {
    20.    while (true) {
    21.       // If flicker set to Intesity OR Both
    22.       if( flickerMethod == flickerMethods.Intensity || flickerMethod == flickerMethods.Both )
    23.       {
    24.         light.intensity = Random.Range( IntensityMin, IntensityMax );
    25.       }
    26.       // If flicker set to Range OR Both
    27.       if( flickerMethod == flickerMethods.Range || flickerMethod == flickerMethods.Both )
    28.       {
    29.         light.range = Random.Range( RangeMin, RangeMax );
    30.       }
    31.        
    32.       yield return new WaitForSeconds (FlickerSpeed);
    33.    }
    34. }
     
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It works fine if you uncomment the "public flickerMethods" line and put in the usual C# fiddly bits ("using UnityEngine;", etc.). I'd recommend Javascript if you want more direct programming without as much cruft.

    --Eric
     
  7. stiltskin

    stiltskin

    Joined:
    Feb 27, 2010
    Posts:
    95
    strange. im still getting a bunch of errors...

    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent( typeof( Light ) )]
    5.  
    6. public enum flickerMethods { Intensity = 0, Range = 1, Both = 2 };
    7. public flickerMethods flickerMethod = flickerMethods.Intensity;
    8.  
    9. // MinIntensity
    10. public float IntensityMin = 0.5f;
    11.  
    12. // MaxIntensity
    13. public float IntensityMax = 0.1f;
    14.  
    15. // MinRange
    16. public float RangeMin = 10.0f;
    17.  
    18. // MaxRange
    19. public float RangeMax = 2.0f;
    20.  
    21. //delay for flickering speed
    22. public float FlickerSpeed = 0.1f;
    23.  
    24. IEnumerator Start () {
    25.    while (true) {
    26.       // If flicker set to Intesity OR Both
    27.       if( flickerMethod == flickerMethods.Intensity || flickerMethod == flickerMethods.Both )
    28.       {
    29.         light.intensity = Random.Range( IntensityMin, IntensityMax );
    30.       }
    31.       // If flicker set to Range OR Both
    32.       if( flickerMethod == flickerMethods.Range || flickerMethod == flickerMethods.Both )
    33.       {
    34.         light.range = Random.Range( RangeMin, RangeMax );
    35.       }
    36.        
    37.       yield return new WaitForSeconds (FlickerSpeed);
    38.    }
    39. }
    the errors:

    Assets/flickeringLight2.cs(7,23): error CS0116: A namespace can only contain types and namespace declarations

    (Line: 7)

    Assets/flickeringLight2_Marc.cs(10,14): error CS0116: A namespace can only contain types and namespace declarations

    (Line: 10)

    Assets/flickeringLight2_Marc.cs(13,14): error CS0116: A namespace can only contain types and namespace declarations

    (Line: 13)

    Assets/flickeringLight2_Marc.cs(16,14): error CS0116: A namespace can only contain types and namespace declarations

    (Line: 16)

    Assets/flickeringLight2_Marc.cs(19,14): error CS0116: A namespace can only contain types and namespace declarations

    (Line: 19)

    Assets/flickeringLight2_Marc.cs(22,14): error CS0116: A namespace can only contain types and namespace declarations

    (Line: 22)

    Assets/flickeringLight2_Marc.cs(24,13): error CS0116: A namespace can only contain types and namespace declarations

    (Line: 24)
     
  8. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You haven't declared the class...look at some other C# scripts.

    --Eric
     
  9. stiltskin

    stiltskin

    Joined:
    Feb 27, 2010
    Posts:
    95
    awesome! thanx a lot man, got it working!
     
  10. Cyclops

    Cyclops

    Joined:
    Jan 31, 2010
    Posts:
    111
    Eric5h5, I have a question about your sample yield code. The OP used two functions, Start() and Update(), with Update() having the yield function. But your sample code moved the yield to the Start(), like this:
    Code (csharp):
    1.  
    2. IEnumerator Start () {
    3.    while (true) {
    4.      yield return new WaitForSeconds (FlickerSpeed);
    5.    }
    6. }
    7.  
    I don't understand the implications of putting a while(true) loop in the Unity Start() function. I would have thought this would prevent Start() from ever completing, and thus Update() from ever running. But I also don't yet fully understand yield :)

    Is the yield returning control to Unity's main execution loop, which runs Update() and then re-runs the loop in Start()? If so, wouldn't this effectively turn Start() into a clone of Update()? Or if Start() keeps running the while loop, when would Update() ever get called?

    I'm so confused... :)

    John C>
    "For all your days, prepare, and meet them ever alike;
    When you are the anvil, bear - when the hammer, strike."
     
  11. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    It does prevent Start from ever completing, because you want it to keep looping forever. Update will run regardless of whether Start ever finishes or not; however, Update isn't necessary, so it doesn't really matter.

    More or less, yes:

    Code (csharp):
    1. function Start () {
    2.    while (true) {
    3.       yield;
    4.    }
    5. }
    That would be a direct replacement for Update, for example, except that you can interrupt it, unlike Update:

    Code (csharp):
    1. function Start () {
    2.    while (true) {
    3.       if (Input.GetButtonDown("Pause")) {
    4.           print ("Loop is on hold for one second");
    5.           yield WaitForSeconds(1.0);
    6.           print ("We now return you to your regularly scheduled loop.");
    7.       }
    8.  
    9.       yield;
    10.    }
    11. }
    --Eric
     
  12. Cyclops

    Cyclops

    Joined:
    Jan 31, 2010
    Posts:
    111
    Thanks, I guess I did understand it :) I was just thinking Update() was more important than it really is...

    John C>
    "For all your days, prepare, and meet them ever alike;
    When you are the anvil, bear - when the hammer, strike."