Delay a prefab instantiate

I am doing a infinite ball rolling game and I have a script to instantiate obstacles for the player to avoid. Here it is:

using UnityEngine;
using System.Collections.Generic;

public class ColliderManager : MonoBehaviour {
	public Transform prefab;
	public int numberOfObjects;
	public float recycleOffset;
	public Vector3 minSize, maxSize, minGap, maxGap;
	public float minx, maxx;
	public Material[] materials;
	public PhysicMaterial[] physicMaterials;
	//public Booster booster;
	private Vector3 nextPosition;
	private Queue<Transform> objectQueue;
	void Start () {
		//GameEventManager.GameStart += GameStart;
		//GameEventManager.GameOver += GameOver;
		//objectQueue = new Queue<Transform>(numberOfObjects);
		//for(int i = 0; i < numberOfObjects; i++){
		//	objectQueue.Enqueue((Transform)Instantiate(prefab, new Vector3(0f, 0f, 0f), Quaternion.identity));
		//enabled = true;
		objectQueue = new Queue<Transform>(numberOfObjects);
		for(int i = 0; i < numberOfObjects; i++){
		nextPosition = transform.localPosition;
		for(int i = 0; i < numberOfObjects; i++){
	void Update () {
		if(objectQueue.Peek().localPosition.z + recycleOffset < Runner.distanceTraveled){
	private void Recycle () {
		Vector3 scale = new Vector3(
			Random.Range(minSize.x, maxSize.x),
			Random.Range(minSize.y, maxSize.y),
			Random.Range(minSize.z, maxSize.z));
		Vector3 position = nextPosition;
		position.z += scale.z * 0.5f;
		position.y += scale.y * 0.5f;
		Transform o = objectQueue.Dequeue();
		o.localScale = scale;
		o.localPosition = position;
		int materialIndex = Random.Range(0, materials.Length);
		o.renderer.material = materials[materialIndex];
		o.collider.material = physicMaterials[materialIndex];
		nextPosition += new Vector3(
			Random.Range(minGap.x, maxGap.x),
			Random.Range(minGap.y, maxGap.y),
			Random.Range(minGap.z, maxGap.z) + scale.z);
		if(nextPosition.x < minx){
			nextPosition.x = minx - minGap.x;
		else if(nextPosition.x > maxx){
			nextPosition.x = maxx - maxGap.x;

The problem is that sometimes when you start the game you run straight into on of the collider obstacles. So my question is if there is a way for me to delay the script for just a few seconds so the player can get going (it’s not exactly fun blowing up on start). Thanks in advance for any help.

simply use Invoke() in Unity to time things and start things off at different times.

it’s a strange thing that many beginners seem not to know about this command (who knows why). it is a basic command you use constantly, all the time, in video games - it’s how you start basically anything happening in your game.

you also have InvokeRepeating, and CancelInvoke

so you might have something like

// have this line of code in say Start() ...
InvokeRepeating( "makeAmazingObstacle", 5, 5 );

function makeAmazingObstacle()
// write your obstacle routine here
// instantiate an obstacle or whatever

Note that inside makeAmazingObstacle, you could well have code like this for example…

Invoke( "hugeFlashingLight", 2.0 );
Invoke( "shakeScreen", 2.1 );
Invoke( "hugeFlashingLight", 2.5 );
Invoke( "changeColoursOfObstacleToRed", 4.75 );
invoke( "getRidOfTheObstacle", 4.95 );

you get it? it’s the basic command you use to make video games.

  1. Add a simple started bool you
    check for, and toggle it on some
    seconds into the game.
  2. Have the
    script disabled on start, then
    enable it a few seconds later.

First off thanks for the response, technically it worked. problem is though is that now the colliders all spawn at once including where the player is. So I guess I’ll have to figure something else out. Thanks again though.

Add this 1st thing in your start

yield WaitForSeconds(2);

Adding this for others’ reference… I tried some of these but used frameCount and modulus instead. This will add a new prefab every time another 20 frames passes.

public class GameController : MonoBehaviour {

	private int count = 0;
	private int limit = 25;
	public GameObject prefab;

	void Update(){
		if (count < limit && Time.frameCount % 20 == 0) {