Spawn clones from a input

Hi!

I have a game were you buy powerups that gives you more score. But at the moment, I have it to spawn in a GameObject but the problem with this is that it is hard to save it. So what I need is to put in a number and it spawn in the amount of powerups that I put in. So if I put in 10 for example, it will spawn 10 power ups. But I don’t know how to do that! So if you know how to do this, please tell me.

Thanks

You could use an IEnumerator in C#.

IEnumerator spawnPowerups(int amount, float timeBetweenSpawns)
{	
	while(amount > 0)
	{
		amount--;
		//put your spawn item line here
		yield return new WaitForSeconds(timeBetweenSpawns); 
	}
}

Then you would call this function like this:

//the first number is how many you want spawned, the second number is how long between each spawn.
    StartCoroutine(spawnPowerups(10,0.3f));

Simple as that :slight_smile:

It should be simple enough to understand how this works. It basically keeps redoing what’s in the while(), until the amount to chose equals zero. It decrements the “amount” by 1 each time it spawns, so it will stop spawning once it has spawned how many you wanted.

(Also, it’s fine to trigger that StartCoroutine again, even while it’s still running. It will merely create another instance of the Coroutine running separate, doing what you wanted it to, while leaving the currently running one alone to continue doing what it needs to. :stuck_out_tongue: )