- Home /
Timed instantiantion not quite working
I've been trying to create a wave effect for my game, so at certain times waves will spawn at specified places... (think side scrolling shooter)
I did have colliders working doing the same job, but they're not ideal for this type of game, and inturn I tried to code a time script so that i can spawn prefabs at locations at certain times...
var wave2 :GameObject;
var wave3 :GameObject;
function Update () {
if(Time.time == 5.0) {
var wave2spawn = Instantiate(wave2, Vector3(-0.9712, 2, -11), Quaternion.identity);
}
if(Time.time == 10.0) {
var wave3spawn = Instantiate(wave3, Vector3(1.237, 2, -9), Quaternion.identity);
}
}
Again, i've probably made fundamental errors, but its difficult to leanr without being taught! ;)
Answer by Mortennobel · Jul 24, 2011 at 12:15 PM
The problem is that Time.time may not exactly be 5.0 or 10.0.
One solution could be to only spawn each object once - and set the wave-variable to null afterwards. And then test using 'larger-than' instead of 'equals. Like this:
var wave2 :GameObject;
var wave3 :GameObject;
function Update () {
if(Time.time > 5.0 && wave2 != null) {
var wave2spawn = Instantiate(wave2, Vector3(-0.9712, 2, -11), Quaternion.identity);
wave2 = null;
}
if(Time.time > 10.0 && wave3 != null) {
var wave3spawn = Instantiate(wave3, Vector3(1.237, 2, -9), Quaternion.identity);
wave3 = null;
}
}
Fantastic mate, will mes aorund with the code now to meet my exact needs, but does exactly what I wanted it to!
$$anonymous$$udos!
Your answer
![](https://koobas.hobune.stream/wayback/20220612154131im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Animated Texture and Instantiation 1 Answer
How to make something happen over time? 1 Answer
increasing rate of spawn on button press 1 Answer
Instantiate clones with error. 1 Answer
Instatiate object in every given second problem C# 2 Answers