- Home /
Instantiate a Prefab Every 5 Seconds - Help!
Hello,
I know this is a newby question:
Is it possible to instantiate a prefab onto my scene every 5 seconds?
I have an empty in where I would like the prefab to spawn in a certain amount of seconds.
Thanks
Answer by duck · Nov 17, 2010 at 10:10 PM
Yes you can do this.
Put this script on an empty gameobject, then drag in the 'prefab' reference. The prefab will be instantiated at the empty gameobject's position.
var prefab : GameObject; var timeDelay = 5;
function Start() { while (true) { yield WaitForSeconds(timeDelay); Instantiate( prefab, transform.position, Quaternion.identity ); } }
Okay, and what would I put if I want to initiate in between random times??
@Oliver Jones: Replace timeDelay with Random.Range() with the parameters you want. On a side note, you could also use InvokeRepeating(). Same effect. Just a different method based entirely on preference.
As Peter G said. Omit the timeDelay var, and just use: yield WaitForSeconds(Random.Range(1,8)); (example - will wait between 1-8 seconds)
Should this really be done in Start() and not in Update() ? I thought Start() was meant to be used to instantiate things and setup the GameObject/Script ?
sir I'm trying to create unlimited time what can i do plz ans