Time being inconsistent

I spawn an object every 2 seconds. It is crucial to be exactly 2 seconds apart. but while running my code it spawns not exactly 2 seconds apart. I tried to make it spawn every x frames, but even this was inconsistent. I can make it by animations but i dont know how to do this. Can this problem be solved? And how?

You can’t get stuff exactly at a fixed time - Unity simply doesn’t update often enough.

The closest thing you can get is the first frame after two seconds have passed, which would be along the lines of:

void SomeEvent() {
    shouldSpawn = true;
    shouldSpawnTime = Time.time;
}

void Update() {
    if(shouldSpawn && Time.time - shouldSpawnTime >= 2f) {
         shouldSpawn = false;
         //spawn
    }
}

If you’re running at X fps, you’ll be at most 1/X seconds off - in the case of 60FPS, that is 0.01666 seconds, or 16 milliseconds.

By the way, even if you could do some hack around where you spawned something at exactly 2 seconds, Unity only redraws things after every Update, so you would only see the change after the same delay, so there’s no point in getting it any closer.