Want to stop instantiating objects when timescale is set to zero

I have my enemies being instantiated from both left and right sides and I've used Time.timescale=0; for the pause menu script so whenever i pause my game everything stop other than the instantiating objects and when i resume the game all the instantiated objects come as bunch. I really need a solution.

• Show us teh codez pl0x! How do you instantiate your enemies? – Vaillancourt Oct 18 at 15:10

just put this line of code on first line of function that instantiates enemies:

  if (Time.timeScale == 0) return;


time scale basically work for objects Movement if you have used delta-Time in their movement function as delta time is multiplied with movement amount.

The other answer will work for your current need, but it will not work when you'll actually start using the timescale for purposes other than pausing the game, such as creating slow motion effects or when you'll want to debug and accelerate the process to attain certain events faster.

The first thing to do is decide the rate at which your enemies spawn: you can use "enemies per minute".

At the initialization, translate this value to a "per second" value.

Then every update, accumulate your deltaTime, and while the accumulator is higher than the spawn rate, spawn an enemy.

Pseudo code:

float spanwRatePerSecond = spawnRatePerMinute/60.0;
float spawnAccumulator = 0.0;

update()
{
spawnAccumulator += Time.deltaTime
while ( spawnAccumulator >= spanwRatePerSecond )
{
spawn()
spawnAccumulator -= spanwRatePerSecond
}
}


Keep in mind that using the timescale variable to pause your game has its disadvantages: at some point, you might want to keep some things moving and animated on the screen when you'll pause the game. Using timeScale = 0 will make it harder to achieve.

Finlay, as it's stated in the documentation, you could prevent the spawning of your enemies by putting the spawn code in the FixedUpdate function, because it is not called when the timeScale is zero.