Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

WaitForSeconds not working how I need it to?

Discussion in 'Scripting' started by nicole.dewinter, May 1, 2015.

  1. nicole.dewinter

    nicole.dewinter

    Joined:
    Nov 6, 2013
    Posts:
    10
    Hi, I have a script;

    Code (csharp):
    1.  
    2. @script RequireComponent(AudioSource)
    3. var minStartWaitTime : float = 5;
    4. var maxStartWaitTime : float = 8;
    5. var minFlickerSpeed : float = 2;
    6. var maxFlickerSpeed : float = 3;
    7. var minWaitTime : float = 8;
    8. var maxWaitTime : float = 10;
    9. var audio2 : AudioSource;
    10. var audio1 : AudioSource;
    11. var startRandom : float = 0;
    12. var endRandom : float = 2;
    13.  
    14.  
    15.  
    16.  
    17. while (true)
    18. {
    19.      GetComponent.<Light>().enabled = true;            //Turns on the light
    20.      if(Random.Range(startRandom, endRandom) < 1) {    // starts an audio
    21.         audio1.Play();
    22.         }
    23.         else {
    24.         audio2.Play();
    25.         }
    26.        
    27.      yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed ));
    28.      GetComponent.<Light>().enabled = false;
    29.      yield WaitForSeconds (Random.Range(minWaitTime, maxWaitTime ));
    30. }
    31.  
    32.  
    It's a script I found online with a bunch of little things I've added to it. I'm trying to create the feeling of a thunderstorm. audio1 and audio2 are two different thunder claps, to keep the sound different.

    The only problem I have with it is the
    Code (csharp):
    1.  
    2.    yield WaitForSeconds (Random.Range(minFlickerSpeed, maxFlickerSpeed ));
    3.  
    The flickerspeed stays the same- less than half a second. The light just flashes on and off even though I have it set to stay on at least 2 seconds.

    I don't know why it isn't working, anyone have any ideas?
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    What are minFlickerSpeed and maxFlickerSpeed set to in the inspector?
     
  3. nicole.dewinter

    nicole.dewinter

    Joined:
    Nov 6, 2013
    Posts:
    10
    ...... 0.01 and 0.1...
    Thankyou!
     
    Kiwasi likes this.
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Your welcome. Remember for public and serialised values, the value in the declaration is overridden by the inspector.