Instantiate a GameObject every second?

Would this be the correct way (or possible way) to instantiate a gameobject every second?

int lastSecond = Time.deltaTime;

if( lastSecond != Time.deltaTime ) // a second has passed (?)
{
     Instantiate( blah );
}
else
{
     lastSecond = Time.deltaTime;
}

Nope…Time.deltaTime is the time since the last frame rather than one second, it would have to be a float rather than an integer, and changes from one frame to the next anyway. How about using InvokeRepeating?

–Eric

Thanks! Only problem I find with this is that it would be difficult to instantiate 8 different prefabs each after each other every second.

InvokeRepeating( "CreateAsteroid1", 0, 1 );
InvokeRepeating( "CreateAsteroid2", 1, 1 );
InvokeRepeating( "CreateAsteroid3", 2, 1 );
InvokeRepeating( "CreateAsteroid4", 3, 1 );
InvokeRepeating( "CreateAsteroid5", 4, 1 );
InvokeRepeating( "CreateAsteroid6", 5, 1 );
InvokeRepeating( "CreateAsteroid7", 6, 1 );
InvokeRepeating( "CreateAsteroid8", 7, 1 );

This creates 8 after each other for the first 8 seconds, but after that I would be instantiating 8 prefabs per second.
Would it be possible to have function pointers, and loop through them like this?

InvokeRepeating( /* LOOP EACH FUNC PTR HERE*/, 0, 1 );

Or something similar?

Or code the above code become something like this?

static int i = 0;
InvokeRepeating( "CreateAsteroid1", i, 1 );
i++;
InvokeRepeating( "CreateAsteroid2", i, 1 );
i++;
InvokeRepeating( "CreateAsteroid3", i, 1 );
i++;
InvokeRepeating( "CreateAsteroid4", i, 1 );
i++;
InvokeRepeating( "CreateAsteroid5", i, 1 );
i++;
InvokeRepeating( "CreateAsteroid6", i, 1 );
i++;
InvokeRepeating( "CreateAsteroid7", i, 1 );
i++;
InvokeRepeating( "CreateAsteroid8", i, 1 );
i++;

Would that cause them each to happen after each? I don’t think it would, but I’m not sure…

EDIT - Would this work?

InvokeRepeating( "CreateAsteroid1", 7, 1 );
InvokeRepeating( "CreateAsteroid2", 6, 2 );
InvokeRepeating( "CreateAsteroid3", 5, 3 );
InvokeRepeating( "CreateAsteroid4", 4, 4 );
InvokeRepeating( "CreateAsteroid5", 3, 5 );
InvokeRepeating( "CreateAsteroid6", 2, 6 );
InvokeRepeating( "CreateAsteroid7", 1, 7 );
InvokeRepeating( "CreateAsteroid8", 0, 8 );

I think what you want maybe

InvokeRepeating( "CreateAsteroid1", 0, 8 ); 
InvokeRepeating( "CreateAsteroid2", 1, 8 ); 
InvokeRepeating( "CreateAsteroid3", 2, 8 ); 
InvokeRepeating( "CreateAsteroid4", 3, 8 ); 
InvokeRepeating( "CreateAsteroid5", 4, 8 ); 
InvokeRepeating( "CreateAsteroid6", 5, 8 ); 
InvokeRepeating( "CreateAsteroid7", 6, 8 ); 
InvokeRepeating( "CreateAsteroid8", 7, 8 );

or since the first parameter is a string not a function

for i in range (0,8):
    InvokeRepeating( "CreateAsteroid${i}", i, 8 );

that code is is boo but the same idea would work in javascript.

Cheers,
Grant

P.S. Do you know how tough it is to answer a programming question here before someone else answers it better.

Hang on, I’m not sure I’m understanding what you’re doing exactly. You want to instantiate 8 different objects, one after the other, and then after they have all been instantiated, there should be no more? If that’s correct, then I’d probably use an array and do something like this:

var asteroid : GameObject[];

function Start () {
     for (i = 0; i < asteroid.Length; i++) {
          Instantiate (asteroid[i]);
          yield WaitForSeconds(1);
     }
}

Then just make the array however big you need it in the inspector, and drag the prefabs onto the slots there.

But if you just want 8 copies of the same object, then don’t bother with the array. InvokeRepeating is for when you want something to keep happening indefinitely, until you stop it, which is what I thought you were after at first.

–Eric

Doh, ryuuguy has what I want… I was over thinking it :stuck_out_tongue:

Thanks for all your guys helps :slight_smile:

I though you want to keep creating them forever. if you just want to create them once use invoke instead invokeRepeating.
to creat one diferent object each second for 8 seconds .i.e.

InvokeRepeating( "CreateAsteroid1", 0, 8 ); 
Invoke( "CreateAsteroid2", 1); 
Invoke( "CreateAsteroid3", 2); 
Invoke( "CreateAsteroid4", 3); 
Invoke( "CreateAsteroid5", 4); 
Invoke( "CreateAsteroid6", 5); 
Invoke( "CreateAsteroid7", 6); 
Invoke( "CreateAsteroid8", 7);

or

for i in range (0,8): 
    Invoke( "CreateAsteroid${i}", i );

cheers,
Grant

Nope, you were correct the first time :wink:

Hmm, it seems that this:
InvokeRepeating( "CreateAsteroid1", 0, 8000 ); Instantiates a new gameobject about once per second, which it definitely shouldn’t…

Works fine here. Well, I assume it does…I’m not waiting around for 2 hours and 13 minutes to see if the next one arrives on time. :wink:

–Eric

making the argument from 8000 to 1 made is go soo fast it froze my computer instantly… Bug maybe?

I’m more inclined to think that your time scale is set to something other than 1. (A lot higher than 1, from the sounds of it.)

–Eric

Ok, I tried setting timeScale to 1, but when I tested the results were similar but not as drastic.