1
\$\begingroup\$

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);
            }
        }
    }
}
\$\endgroup\$

marked as duplicate by DMGregory unity Nov 28 '17 at 14:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • \$\begingroup\$ So you want to turn both of those methods into a single method? \$\endgroup\$ – Draco18s Nov 27 '17 at 22:49
  • \$\begingroup\$ Have you tried wrapping the yield in an if? Also why do you have two yields back to back? \$\endgroup\$ – Stephan Nov 27 '17 at 23:15
  • \$\begingroup\$ @Draco18s Right. \$\endgroup\$ – jdoedoe doedoe Nov 27 '17 at 23:36
  • \$\begingroup\$ @Stephan but it's IEnumerator type so how can i use IF inside the method ? I need also to make some IF that will change the method type from IEnumerator to void i think. \$\endgroup\$ – jdoedoe doedoe Nov 27 '17 at 23:59
  • 1
    \$\begingroup\$ All you need is a wrapper method that either calls the StartCoroutine or fast-forwards the IEnumerator in one go. See the linked duplicate for details. \$\endgroup\$ – DMGregory Nov 28 '17 at 14:04

Browse other questions tagged or ask your own question.