- Home /
Spawn Controller...
My code is suppose to spawn prefab object 10 times and stop till one object is destroyed. Problem is that it only spawns one and stops. Any Ideas?
var game_cube : Rigidbody;
var cube_count = 0;
InvokeRepeating("LaunchProjectile", 2, 60);
function Update() {
if (cube_count>= 10) {
CancelInvoke();
}
}
function LaunchProjectile () {
instance = Instantiate(game_cube,transform.position, transform.rotation);
instance.velocity = transform.TransformDirection( Vector3( 0, 0, 0 ) );
cube_count = cube_count +1;
}
Answer by aldonaletto · Sep 28, 2011 at 11:13 PM
It seems you're instantiating projectiles one over another, because you're assigning 0,0,0 to the rigidbody.velocity - they will just fall to the ground if useGravity is set. The rest of the code seems ok, thus you may simply change the velocity setting to:
instance.velocity = transform.forward;
This will give a 1m/s speed in the projectile forward direction.
Answer by testure · Sep 28, 2011 at 11:13 PM
If I had to guess it's because you're invoking a new projectile once per minute and not waiting to see if a second one ever spawns.
Change 60 to something lower (like 1) and see if it suddenly works.