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());
}
}
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.
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");
}
}
}