issue with the update routine

I have created a quad, and made it a prefab. Now I need to clone it one after another at equal interval of time and space. I check for a time period of 0.2 seconds and clone the next set of prefabs. but this is what happens. the first few clones are not fine. the time gap between clones is erratic and seems out of control. advice me for a solution.


The frame rate in your computer changes a lot (depending on what are you doing.

How are you doing your time calculation? Using Time.time or Time.deltatime?

im using Time.deltatime . and the routine is the same. it works fine after the first few quads are clones as you can see :frowning:

If you provide more information like the code, it will be easier to help you out. :slight_smile:

the prefab is a quad. in update routine i check for 0.2 seconds delay and clone it. the quads has a script component which makes it move to the left

transform.position += m_direction * m_speed * Time.deltaTime; // this is how the quads move (update routine of the quad)

timeInterval += Time.deltaTime;
if (timeInterval > 0.5) {
redRunner = Instantiate (Resources.Load (“Prefabs/TRed”), instantiantionPos, Quaternion.identity) as GameObject;} // this is where i clone objects in update routine of an empty game object.

the direction is left and i clone them at the right end of the play area. sorry i dint mention about the transforms previously

Any special reason why you use Resources.Loads?

Also, in your code examples - which are much better to read with code tags - you finish the if-statement without resetting the timeInterval variable to 0. This will make your game load (from resources) and instantiate the quad every frame, which can be quite heavy.
I’d recommend to simply make a public variable of type GameObject and assign your prefab to it. If you only need a certain amount of these quads, you could also use a pooling technique which additionally avoids all the instantiation in Update, so you can just re-use quads again by moving them around when they’re no longer needed.

thanks for you advice on code tags.

I am resetting the variable to 0 in my program and i will consider the advice on pooling technique.

but still i dint get it why the gap between the first few instances of the quad are uneven which is quite even later on. :frowning:

Ah okay.
I tested this myself and it seems to work just fine. Note that i also just used instantiate for this example, but in a project it does make sense to avoid instantiation.

This is the script which spawns the prefabs.

private float timeSinceInstantiation = 0f;

    public float interval = 1f;
    public GameObject prefab;
    public Vector3 instantiantionPos = Vector3.zero;


    void Update()
    {
        timeSinceInstantiation += Time.deltaTime;
        if (timeSinceInstantiation >= interval)
        {
            Instantiate(prefab, instantiantionPos, Quaternion.identity);
            timeSinceInstantiation = 0;
        }
    }

The script on the prefab contains the code you posted above in order to move the objects.

1 Like

You’re probably experiencing some sort of large overhead when your game initially starts. This causes Time.deltaTime to be a higher than normal value which means your object moves at a larger interval. If the spacing needs to be even then only use deltaTime to measure time and keep a count of how may things you have spawned and then derive the position by multplying that count by the spacing.

1 Like

thank you for your responses. may be the overhead issue should be sorted out. i will consider @KelsoMRK advice on spacing, thanks @Suddoha for the pooling technique. i hope it will work fine. will test it :slight_smile: