- Home /
Solved it myself
trouble looping through voicelines using Coroutine
why does this not work?
public Transform Robot;
public Transform player;
protected FMOD.Studio.EventInstance RobotVoice;
public string[] voicelines;
private void Start()
{
//for(int i = 0; i < voicelines.Length; i++)
//{
// StartCoroutine(LateStart(2, voicelines[i]));
// i++;
//}
foreach (string line in voicelines)
{
StartCoroutine(LateStart(2, line));
}
}
IEnumerator LateStart(float waitTime, string voicepath)
{
float Timer = 0;
RobotVoice = FMODUnity.RuntimeManager.CreateInstance(voicepath);
RobotVoice.set3DAttributes(FMODUnity.RuntimeUtils.To3DAttributes(Robot.position));
RobotVoice.start();
while (Timer < waitTime)
{
Timer += Time.deltaTime;
FMODUnity.RuntimeManager.SetListenerLocation(player);
yield return 0;
}
RobotVoice.stop(FMOD.Studio.STOP_MODE.ALLOWFADEOUT);
RobotVoice.release();
Timer = 0;
}
Answer by Baalhug · Sep 26, 2018 at 02:34 AM
The first thing I see: you are using a while loop with a condition that never changes, because both variables Timer and waitTime are defined in the coroutine itself or passed as parameters. When processor gets to yield return line, it exits the coroutine and casts all the cycle events like update function, when all those events are executed in 1 cycle, it returns to the coroutine, but it will never leave the while loop, the code below that loop will never be executed. So, if you want to end that loop you must change one or both those variables values inside the loop or in any of those event cycle functions like update.
Anyway, I don't know what you mean when you say it does not work.
(EDIT: Ok, after you added:
Timer += Time.deltaTime;
I don't know why it does not work)