- Home /
instantiation at interval times
i would like to have a gameObject be instantiated every ten seconds or so. the ten will change because it will be a variable that will randomly choose a number between ten and 15. i want to make the gameobject instantiate at an interval of the variable that contains the random number, how would i go about doing this? thank you.
Answer by testure · Jun 19, 2011 at 05:02 AM
use coroutines: http://unity3d.com/support/documentation/ScriptReference/index.Coroutines_26_Yield.html
Answer by RodrigoSeVeN · Jun 19, 2011 at 07:04 AM
As coroutines may be troublesome sometimes, as you need to watch were they are called, you should always have them on a created function.
Coroutines are very optimized as calls, but if you have some trouble setting it up, here's another possibility to try in the meantime: to create a "timer" and an "interval" variable, and do something like this:
var intervalTimer=0.0; var interval=0.0;
function Start(){
intervalTimer=Time.time;
interval=Random.Range(10,15);
}
function Update(){
if(intervalTimer+interval>=Time.time){
Instantiate(myObject);
intervalTimer=Time.time;
interval=Random.Range(10,15);
}
}
If you want to drop the interval
variable, you can just remove it completely and replace it with Random.Range(10,15)
in that if()
statement. It is very recommended to learn to use coroutines, so don't stick for too long with this simple solution as it will get costly in bigger applications.
I'm gonna have to disagree here. Anytime you want to do something at a regular interval and use math in the update method, you're chipping away at your performance. When you use a coroutine, the method isn't even invoked until the next interval, so those cycles aren't wasted.
It's true that a handful interval checks like this won't impact performance in the slightest- but a full featured game is likely to have dozens if not hundreds of coroutines running on a variety of gameObjects. Not to mention- depending on the platform, the cycles you save by using coroutines could actually give you a noticeable framerate increase (an iPhone game I worked on a couple years ago benefited from a 15FPS gain by switching to coroutines). So that's why I say it's bad practice to jump in using interval checks, and ins$$anonymous$$d- suck it up and learn how to use a coroutine properly :)
You are absolutely correct. One must not risk performance on a big project like that. But then again, no offenses to the original poster, an experienced coder will know how to work with intervals. That's why i offered a simple solution with some quick code, as making mistakes and learning from them is also part of the deal.
$$anonymous$$y "troublesome" comment was due to the specific usage of coroutines(yield) when dealing with the update function for example.
Yeah, aside from performance, coroutines are quite a bit simpler than convoluted code using Update.
function Start () {
while (true) {
yield WaitForSeconds (Random.Range(10.0, 15.0));
Instantiate (myObject);
}
}
See how much easier that is? I'm not aware of any cases where they're troublesome...there are a few functions like Update that can't be coroutines, but the point is that you wouldn't use Update anyway.
Hi - I'm sorry - just trying to understand this code above. While what is true? If I put this code into my start function and log it, nothing happens. If I follow the coroutines examples in unity's scripting reference, nothing loops. The coroutine runs, but there is no way to call it again from within itself. How do you loop? I don't want the routine to be triggered by anything, I just want it to do something every few seconds.
While what is true?
While true is true. Which will be forever, or until the laws of the universe change, whichever comes first.
If I put this code into my start function and log it, nothing happens.
Log what? If you copy/paste the code I wrote, and run it, it works. (Though you need to add var myObject : GameObject;
in order to have something to instantiate. You might also lower the values in Random.Range, so you don't have to wait so long.)
The coroutine runs, but there is no way to call it again from within itself.
That would be recursion, which you normally don't want, although it can be useful sometimes.
How do you loop? I don't want the routine to be triggered by anything, I just want it to do something every few seconds.
You loop in the usual ways: a for
or while
loop.
Answer by jacob johnston · Jun 20, 2011 at 11:33 PM
this is all great. thank you. the thing i always have trouble with though is that you cannot run a coroutine in an update function but i want to be checking it throughout the gameplay at all times. other ways around this seem to escape me.
Please don't post comments as answers. The point of coroutines is that you don't use Update at all. See the code I posted under SeVeNR$$anonymous$$N's answer.
ok i think i understand now. i do not use coroutines in update functions but i can run a function of my own inside the update function that contains a coroutine or i could use a start function.
No, just ditch Update entirely. You don't need it for this.