Instantiate at Intervals

Hey guys,

I need to instantiate a prefab at a given time interval. I have tried at least 20 different ways, and not one single solution I’ve found anywhere on the internet has worked. Here’s what happens: a prefab gets successfully instantiated after waiting 2 seconds, but then it gets instantiated again every single frame, giving me tons and tons of them. Not what I need. This result has been the same for every method - using a coroutine, using invoke repeating…you name it, I’ve tried it.

I’m using C#. Any help is greatly appreciated.

Thank you!

private float InstantiationTimer = 2f;

void Update () {
	CreatePrefab();
}

void CreatePrefab()
{
	InstantiationTimer -= Time.deltaTime;
	if (InstantiationTimer <= 0)
	{
		Instantiate(prefab, spawn.position, Quaternion.identity);
		InstantiationTimer = 2f;
	}
}

You could change the 2f to a variable for easier control of the wait time if desired.

You are calling “InvokeRepeating” in update that’s why it is happening. You need to call “InvokeRepeating” only once and that will work according to you what you are saying. Read Invoke repeating reference carefully. Call it into Start.