- Home /
 
               Question by 
               dreasgrech · Mar 11, 2013 at 08:44 PM · 
                c#yieldienumeratorrefactor  
              
 
              Having trouble refactoring an IEnumerator method with multiple yields
The gist of my code is as follows:
 // Play the first beat
 audio.PlayOneShot(beat);
 
 // Show 1st heartbeat border flash
 TweenAlpha.Begin(heartbeatPanel.gameObject, 0.1f, currentStress);
 yield return new WaitForSeconds(0.1f);
 TweenAlpha.Begin(heartbeatPanel.gameObject, 0.5f, 0);
 
 yield return new WaitForSeconds(interval);
 
 // Play the second beat
 audio.PlayOneShot(beat);
 
 // Show 2nd heartbeat border flash
 TweenAlpha.Begin(heartbeatPanel.gameObject, 0.1f, currentStress);
 yield return new WaitForSeconds(0.1f);
 TweenAlpha.Begin(heartbeatPanel.gameObject, 0.5f, 0);
 
 yield return new WaitForSeconds(interval * 2);
Now I want to split the above code into a single IEnumerator method with 2 calls.
This is what I came up with:
 StartCoroutine(PlayBeat(currentStress, interval));
 StartCoroutine(PlayBeat(currentStress, interval * 2));
 // ...
 IEnumerator PlayBeat(float currentStress, float interval)
 {
      audio.PlayOneShot(beat);
      TweenAlpha.Begin(heartbeatPanel.gameObject, 0.1f, currentStress);
      yield return new WaitForSeconds(0.1f);
      TweenAlpha.Begin(heartbeatPanel.gameObject, 0.5f, 0);
      yield return new WaitForSeconds(interval);
 }
The problem with this is that it doesn't work, hence why I'm asking the question.
What's the best way to extract my two repetitive blocks of code above into a single IEnumerator method?
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by dreasgrech · Mar 13, 2013 at 07:19 PM
Solved it, thanks to StackOverflow.
What I was missing was yielding the Coroutine:
 yield return StartCoroutine(PlayBeat(currentStress, interval));
 yield return StartCoroutine(PlayBeat(currentStress, interval * 2));
Your answer
 
 
             Follow this Question
Related Questions
What am I doing wrong with IEnumerator? 1 Answer
Problem using yield and IEnumerator in a custom function 2 Answers
IEnumerator not working 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                