Time Delay between buttons created from for loop not working

I am trying to create 10 buttons with a 1 second delay in-between the creation of each one and they all get created but there is no delay… trying to figure out what I need to change in order to make the delay work

public class CreateTenButtons : MonoBehaviour
{
int x;
public System.Random rnd = new System.Random();

  IEnumerator WaitASec()
    {
    yield return new WaitForSeconds(1);
    }

    void Start ()
    {
    for (x = 0; x <= 10; x++)
            {

  GameObject mCanvas = GameObject.Find("Canvas");
  GameObject button = new GameObject();
  button.AddComponent<CanvasRenderer>();
  button.AddComponent<RectTransform>();
 
  Button mButton = button.AddComponent<Button>();
  Image mImage =  button.AddComponent<Image>();
  mButton.targetGraphic = mImage;
  button.transform.position = new Vector3(rnd.Next(0, 800), rnd.Next(0, 600), 0);
  button.transform.SetParent(mCanvas.transform);
  button.GetComponent<Button>().onClick.AddListener(Testmethod);

  StartCoroutine(WaitASec());

            }
    }

When you start a coroutine, the current function doesn’t wait for the coroutine to finish, it continues as soon as the coroutine pauses.

Move all of your button-creation code into the coroutine.

2 Likes

That’s not how coroutines work. They don’t affect code outside of themselves with their yield statements. You need to put the stuff you want to delay inside the coroutine, after the yield statement to make it run after the delay.

1 Like

Thanks! I’ll try it!

Thanks! I’ll try it!

Ok now it waits the 2 seconds and then creates all the buttons all at once… I am trying to make a delay in-between each button getting created and think my for loop may be in the wrong place… Any idea?

public class CreateTenButtons : MonoBehaviour
{
int x;
public System.Random rnd = new System.Random();

private IEnumerator coroutine;


    void Start ()
    {

    for (x = 0; x <= 10; x++)
   
    {
        coroutine = WaitAndPrint(2.0f);
        StartCoroutine(coroutine);

        print("Coroutine started");
   

    IEnumerator WaitAndPrint(float waitTime)
    {

    yield return new WaitForSeconds(waitTime);

    GameObject mCanvas = GameObject.Find("Canvas");
    GameObject button = new GameObject();
   
    button.AddComponent<CanvasRenderer>();
    button.AddComponent<RectTransform>();
   
    Button mButton = button.AddComponent<Button>();
    Image mImage =  button.AddComponent<Image>();
    mButton.targetGraphic = mImage;
   
    button.transform.position = new Vector3(rnd.Next(0, 800), rnd.Next(0, 600), 0);
    button.transform.SetParent(mCanvas.transform); 
    button.GetComponent<Button>().onClick.AddListener(Testmethod);
   
    print("Coroutine ended");
    }
    }
}

Ok now it waits the 2 seconds and then creates all the buttons all at once… I am trying to make a delay in-between each button getting created and think my for loop may be in the wrong place… Any idea?

public class CreateTenButtons : MonoBehaviour
{
int x;
public System.Random rnd = new System.Random();

private IEnumerator coroutine;


    void Start ()
    {

    for (x = 0; x <= 10; x++)
   
    {
        coroutine = WaitAndPrint(2.0f);
        StartCoroutine(coroutine);

        print("Coroutine started");
   

    IEnumerator WaitAndPrint(float waitTime)
    {

    yield return new WaitForSeconds(waitTime);

    GameObject mCanvas = GameObject.Find("Canvas");
    GameObject button = new GameObject();
   
    button.AddComponent<CanvasRenderer>();
    button.AddComponent<RectTransform>();
   
    Button mButton = button.AddComponent<Button>();
    Image mImage =  button.AddComponent<Image>();
    mButton.targetGraphic = mImage;
   
    button.transform.position = new Vector3(rnd.Next(0, 800), rnd.Next(0, 600), 0);
    button.transform.SetParent(mCanvas.transform); 
    button.GetComponent<Button>().onClick.AddListener(Testmethod);
   
    print("Coroutine ended");
    }
    }
}

Don’t double-post.

When I said to move the button-creation code, I meant including the loop. The only thing in Start should be StartCoroutine(whatever).

1 Like

Oh, ok. Thank you!

And yeah sorry about the double-posting… I wasn’t sure who would be around still

It works now!! Thank you very much!!