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?