- Home /
Loop Coroutine For A Demo Mode
I'm working on doing a demo mode and I've come up with this method for looping a coroutine. I'm just curious??? Is this an OK method to use or should I be using something different?? my little demo project seems to work OK... but just what to get some of you more experienced people's opinion on this method.
Thanks. :) Here is my WIP proof of concept script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class demoLoop : MonoBehaviour {
public GameObject ImageOne;
public GameObject ImageTwo;
public GameObject SounderObj;
public AudioClip Voice01;
public bool DemoMode = false;
public void SetDemoModeOnOff()
{
if (DemoMode == false)
{
DemoMode = true;
}
else if (DemoMode == true)
{
DemoMode = false;
}
}
public void PlayDemoOne()
{
StartCoroutine(StartDemoOne());
}
IEnumerator StartDemoOne()
{
yield return new WaitForSeconds(2.0f); // wait time
if (DemoMode == true)
{
yield return new WaitForSeconds(1.5f); // wait time
ImageOne.SetActive(true);
yield return new WaitForSeconds(3.5f); // wait time
ImageOne.SetActive(false);
ImageTwo.SetActive(true);
yield return new WaitForSeconds(3.5f); // wait time
SounderObj.GetComponent<AudioSource>().PlayOneShot (Voice01, 1);
yield return new WaitForSeconds(3.5f); // wait time
ImageTwo.SetActive(false);
PlayDemoOne ();
}
}
}
Answer by slavo · Dec 14, 2017 at 04:48 PM
Your method is ok. You can simplified your loop to
IEnumerator StartDemoOne() { while (DemoMode == true) { yield return new WaitForSeconds(2.0f); // wait time if (DemoMode == false) yield break; yield return new WaitForSeconds(1.5f); // wait time ImageOne.SetActive(true); yield return new WaitForSeconds(3.5f); // wait time ImageOne.SetActive(false); ImageTwo.SetActive(true); yield return new WaitForSeconds(3.5f); // wait time SounderObj.GetComponent<AudioSource>().PlayOneShot (Voice01, 1); yield return new WaitForSeconds(3.5f); // wait time ImageTwo.SetActive(false); //PlayDemoOne (); --> no need to start coroutine again } }
Thanks @slavo I had seen the "While true" before but was not too sure how that was used. Thank you so much for clearing that up for me :) That was greatly appreciated :)
Your answer
Follow this Question
Related Questions
Coroutines and states 1 Answer
Toggling bools automatically using coroutines 1 Answer
Is there a way to find out when a coroutine is done executing? 3 Answers
Executing coroutines consecutively 0 Answers