I'm generating a level, and I have an optional delay to show the steps level generation one by one.
If my generationDelay
boolean is true, I want to use StartCoroutine to spread the generation out over time. If it's false, I want to call the same method but without StartCoroutine.
The problem is that the method is type IEnumerator.
I can make two methods one type of IEnumerator and one not. But I wonder if there is a way to use one method.
In the first script top i did:
public bool generationDelay = false;
Then in a method:
private void BeginGame()
{
mazeInstance = Instantiate(mazePrefab) as Maze;
if (generationDelay == true)
{
StartCoroutine(mazeInstance.Generate(generationDelay));
}
else
{
mazeInstance.Generate(generationDelay);
}
}
Then the other script where the method Generate is: In the top:
public float generationStepDelay = 0.01f;
Then the method Generate:
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);
}
}
I'm getting an error on the line:
WaitForSeconds delay = new WaitForSeconds(generationStepDelay);
"Embedded statement cannot be a declaration or labeled statement"
And an error on the line:
yield return delay;
"The name 'delay' does not exist in the current context"
The reason I want to use a bool variable is that when i'm using StartCoroutine it's kind of slow, even if I change the value of generationStepDelay
to 0 it's kind of slow. And if I'm not using StartCoroutine and not using the Generate method as IEnumerator it will work fast.
So i wonder what should I do and how?