- Home /
WaitForSeconds Question
Hi I'm trying to get my code to wait for a certain amount of time in this function that gets called when a UI Button is clicked.
 public function PlayRecording(){
     for(var i:int = 0; i < currentArrayPlace; i++)
     {
        playBackSound = Network.Instantiate(soundObject,transform.position,transform.rotation,0);
        if(recordingArrayInstruments[i] == "Piano"){
                playBackSound.audio.clip = pianoSoundClip;
        }
        playBackSound.audio.pitch = float.Parse(recordingArrayPitches[i]);
        playBackSound.transform.tag = recordingArrayInstruments[i] + recordingArrayPitches[i];
        playBackSound.audio.loop = false;
        playBackSound.audio.Play();
        if(i != 4){
                //I WANT IT TO WAIT HERE!!!!!!!!!!!!!!!!!!
        }
     }
 }
How can I do this, as I'm completely stumped. I realise similar questions have been asked before but I can't seem to incorporate their answers into mine.
Answer by Mons1999 · May 26, 2015 at 11:13 PM
Use a coroutine.
http://docs.unity3d.com/ScriptReference/WaitForSeconds.html
You have to call your function like this : StartCoroutine(YouFunctionName()). The function called must be a IEnumerator, and you put the time to wait in the WaitForSeconds function.
Here's an example based on your code.
  public function PlayRecording(){
      for(var i:int = 0; i < currentArrayPlace; i++)
      {
    [...]
         if(i != 4){
              StartCoroutine(WaitForSomething());
 
         }
      }
  }
     
     private IEnumerator WaitForSomething() {
         yield return new WaitForSeconds(time you want to wait);
         >>Do what you want
    }
Please check answered if this helped ;)
This is what I've got now , and it still plays all sounds at once with no delay/wait.
 public function PlayRecording(){
     for(var i:int = 0; i < currentArrayPlace; i++)
     {
        playBackSound = Network.Instantiate(soundObject,transform.position,transform.rotation,0);
        if(recordingArrayInstruments[i] == "Piano"){
                playBackSound.audio.clip = pianoSoundClip;
        }
        playBackSound.audio.pitch = float.Parse(recordingArray$$anonymous$$ches[i]);
        playBackSound.transform.tag = recordingArrayInstruments[i] + recordingArray$$anonymous$$ches[i];
        playBackSound.audio.loop = false;
        playBackSound.audio.Play();
        if(i != 4){
                //WaitForTime(recordingArrayTimes[i]); 
                StartCoroutine(WaitForTime(recordingArrayTimes[i+1]));
                //yield WaitForSeconds(recordingArrayTimes[i+1]);
        }
     }
 }
 
 public function WaitForTime(timeToWait:float) {
       yield WaitForSeconds(timeToWait);
 }
You have to put what to do after the "yield WaitForSeconds(timeToWait);" line. Can you just explain a little bit what you want this script to do ? Because yes a for() loop is calculated in the same frame so without any delay. If you want a delay you'd better use a if() statement in the Update() function.
So here's a code example. I usually code in C# so there might be errors but this is the main idea :
function Start(){ i=0; StartCoroutine(WaitForTime); }
  private function DoWhatINeedToDo(){
      playBackSound = Network.Instantiate(soundObject,transform.position,transform.rotation,0);
     if(recordingArrayInstruments[i] == "Piano"){
             playBackSound.audio.clip = pianoSoundClip;
     }
     playBackSound.audio.pitch = float.Parse(recordingArray$$anonymous$$ches[i]);
     playBackSound.transform.tag = recordingArrayInstruments[i] + recordingArray$$anonymous$$ches[i];
     playBackSound.audio.loop = false;
     playBackSound.audio.Play();
     if(i != 4){
             //WaitForTime(recordingArrayTimes[i]); 
             StartCoroutine(WaitForTime(recordingArrayTimes[i+1]));
             //yield WaitForSeconds(recordingArrayTimes[i+1]);
     }
  }
  
  public function WaitForTime(timeToWait:float) {
        yield WaitForSeconds(timeToWait);
        if(i!=4){
            i++;
            DoWhatINeedToDo();
            StartCoroutine(WaitForTime());
        }
  }
You first call in Start(), then you increase i each timeToWait, and you call again a function that does whatever you want to do. Let me know if this helped ;)
Answer by tkoknordic · May 27, 2015 at 12:42 PM
Using a coroutine is one way and other is Invoke:
 public function PlayRecording(){
      for(var i:int = 0; i < currentArrayPlace; i++)
      {
         playBackSound = Network.Instantiate(soundObject,transform.position,transform.rotation,0);
         if(recordingArrayInstruments[i] == "Piano"){
                 playBackSound.audio.clip = pianoSoundClip;
         }
         playBackSound.audio.pitch = float.Parse(recordingArrayPitches[i]);
         playBackSound.transform.tag = recordingArrayInstruments[i] + recordingArrayPitches[i];
         playBackSound.audio.loop = false;
         playBackSound.audio.Play();
         if(i != 4){
                 //I WANT IT TO WAIT HERE!!!!!!!!!!!!!!!!!!
                 Invoke("DoRestOfStuff", recordingArrayTimes[i+1]);
         }
      }
  }
  
  public function DoRestOfStuff(){
     //do something here
  }
Your answer
 
 
             Follow this Question
Related Questions
WaitUntil Combined With sqrMagnitude is not working? 1 Answer
C# Coroutine help 1 Answer
Wierd issue with Coroutines? 2 Answers
restart level when dead with delay 3 Answers
Coroutine not running after yield return new WaitForSeconds 3 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                