Pause in c#

Does anyone know how to make a script wait, e.x.
Im instantiating alot(10) cubes, but i want them to instantiate with an interval of 1 sec.

Im thinking the WaitForSeconds will work, but dont know how to use it properly.

This is my script

using UnityEngine;
using System.Collections;

public class SpawnPlayer : MonoBehaviour {
	public Transform cubie;
	void Start(){
		for(int i = 0; i < 10; i++){
			Instantiate(cubie,new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
		}
	}
}

I dont want straight answers, would rather like tips, and ideas.

Thanks in advance.
-Jonas

Hi,

Try something like this:

yield return new WaitForSeconds(5);

thatll wait for 5 seconds, have a play around with it for the effect you want :slight_smile:

Allready tried that, gives me this error:
“The body of SpawnPlayer.Update()' cannot be an iterator block because void’ is not an iterator interface type”

-Jonas Rasmussen

look for IENumerator. this helps you with pause in c#.

Use like this:

StartCoroutine(CreateCubes);
using UnityEngine;
using System.Collections;

public class SpawnPlayer : MonoBehaviour
{
    public float WaitingTime = 6f;
    public Transform cubie;

    IEnumerator CreateCubes()
    {
        for(int i = 0; i < 10; i++)
        {
            Instantiate(cubie,new Vector3(i * 2.0f, 0, 0), Quaternion.identity);
            yield return new WaitForSeconds(WaitingTime);
        }
    }
}

You can also spawn in the Update instead, keeping track of the time since you last created a cube.
When that time is >= Time Between Spawns, then reset it.