Pause in scripting

Hello my friends! In creating my own Runner game I’ve faced some problems!

Look at my script:

        public Transform[] prefab = new Transform[12];
	public Transform place;
		
	void Update()
	{		
		Vector3 CarPos = gameObject.transform.position;
		CarPos.z = Monkey.distance + 30;
		
		Instantiate(prefab[Random.Range(0, 12)], CarPos, place.rotation);
		// I want some pause here, about 3 seconds	
				
	}

The point is, that these prefabs should appear in fornt of my runner - monkey. But because of Update function, they appear with every new frame. So I want to write something like: Pause 3seconds, and new object will appear 3 seconds later.
So is it possible to make a pause in script?

It is possible, do you know coroutines? You should read about them in case you do not know about them. They are a little bit tricky to use the first few times, but they will help you with what you want.

Another hint: WaitForSeconds :slight_smile:

Well, coroutines can be used only in specific statements, if I’m not mistaken. But is it possible to use it in a simple script?

It is, you dont even need the Update method for it, just call InvokeRepeating with your Coroutine and it should work.

Your Coroutine may then look like something like this:

void Do()
{
  Vector3 CarPos = gameObject.transform.position;
  CarPos.z = Monkey.distance + 30;
  Instantiate(prefab[Random.Range(0, 12)], CarPos, place.rotation);
}

Sure, it is really awesome! Thank you very much!