- Home /
How can i use a bool variable to decide if to use a StartCoroutine ?
In one script at the top:
public bool generationDelay = false;
Then in a method, there was only StartCoroutine i added all the IF statement using the bool variable:
private void BeginGame()
{
mazeInstance = Instantiate(mazePrefab) as Maze;
if (generationDelay == true)
{
StartCoroutine(mazeInstance.Generate(generationDelay));
}
else
{
mazeInstance.Generate(generationDelay);
}
}
Then in another script in the Generate method:
public IEnumerator Generate (bool generationDelay) {
if (generationDelay == true)
WaitForSeconds delay = new WaitForSeconds(generationStepDelay);
cells = new MazeCell[size.x, size.z];
List<MazeCell> activeCells = new List<MazeCell>();
DoFirstGenerationStep(activeCells);
while (activeCells.Count > 0) {
yield return delay;
DoNextGenerationStep(activeCells);
}
}
When i added this line inside Generate:
if (generationDelay == true)
I'm getting error on the line:
WaitForSeconds delay = new WaitForSeconds(generationStepDelay);
The error message:
Embedded statement cannot be a declaration or labeled statement
And also error on the line: On the delay
yield return delay;
The name 'delay' does not exist in the current context
What i want is a public bool variable to decide if to use or not StartCoroutine.
Answer by Komayo · Aug 18, 2017 at 06:56 PM
You need to declare this variable "generationStepDelay" before using it.
float generationStepDelay = 5.0f; // 5 seconds
then on "yield return delay;" just type
yield return new WaitForSeconds(generationStepDelay);
Your answer
Follow this Question
Related Questions
How can i lock the mouse cursor in the middle of the screen ? 1 Answer
Why InputField don't have the property text ? 2 Answers
How can i change the walls height and keep the size for example 100x100 but height 0.1 ? 1 Answer
How can i using a break point if a gameobject have a collider after added to it ? 1 Answer
How can i get all childs from List ? 3 Answers