Now I'm using two methods, one as StartCoroutine and one not.
What I want to do is to have just one method and when I set the StartCoroutine variable to true or false when the game is running I want it to switch between the coroutine and the main. Is there a simple way to do it with one method and not two, if not, how do I achieve this?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Grid : MonoBehaviour
{
public GameObject gridBlock;
public int worldWidth = 10;
public int worldHeight = 10;
public bool StartCoroutine = false;
public float spawnSpeed = 0;
void Start()
{
if (StartCoroutine)
{
StartCoroutine(GenerateGridSC());
}
else
{
GenerateGrid();
}
}
private void GenerateGrid()
{
for (int x = 0; x < worldWidth; x++)
{
for (int z = 0; z < worldHeight; z++)
{
GameObject block = Instantiate(gridBlock, Vector3.zero, gridBlock.transform.rotation) as GameObject;
block.transform.parent = transform;
block.transform.name = "Block";
block.transform.tag = "Block";
block.transform.localPosition = new Vector3(x, 0, z);
}
}
}
IEnumerator GenerateGridSC()
{
for (int x = 0; x < worldWidth; x++)
{
yield return new WaitForSeconds(spawnSpeed);
for (int z = 0; z < worldHeight; z++)
{
yield return new WaitForSeconds(spawnSpeed);
GameObject block = Instantiate(gridBlock, Vector3.zero, gridBlock.transform.rotation) as GameObject;
block.transform.parent = transform;
block.transform.name = "Block";
block.transform.tag = "Block";
block.transform.localPosition = new Vector3(x, 0, z);
}
}
}
}