Search Unity

time gaps between ienumerator doesnt work ... and wierd audio bugs

Discussion in 'Scripting' started by wisien92, May 9, 2014.

  1. wisien92

    wisien92

    Joined:
    Nov 16, 2012
    Posts:
    56
    Hi i have been trying to write a script for thunder... it would do itself once and then wait for random time to do again but.... it waits the first 5F in WaitForSec and then repeats itself like AK47 .... also the audio.PlayOneShot(); works strangly ... dunno why

    Code (csharp):
    1.  
    2. public class ThunderStorm : MonoBehaviour {
    3.  
    4.     public AudioClip[] thunderClap;
    5.     int randomThunder;
    6.    
    7.     // Update is called once per frame
    8.     void Update () {
    9.         StartCoroutine(ThunderHandler());
    10.     }
    11.  
    12.     IEnumerator ThunderHandler() {
    13.  
    14.         yield return new WaitForSeconds(5F);
    15.         audio.PlayOneShot(thunderClap[Random.Range(0,thunderClap.Length)]);
    16.         light.intensity = 0.8F;
    17.         yield return new WaitForSeconds(0.4F);
    18.         light.intensity = 0.0F;
    19.         yield return new WaitForSeconds(0.2F);
    20.         light.intensity = 0.6F;
    21.         yield return new WaitForSeconds(0.3F);
    22.         light.intensity = 0.0F;
    23.  
    24.         float random = Random.Range(80F, 220F);    
    25.         yield return new WaitForSeconds(random);
    26.         audio.Stop();
    27.     }
    28. }
    29.  
     
  2. Deleted User

    Deleted User

    Guest

    its because you are calling your coroutine every frame