# How do I increase the spawn rate of an object as the “difficulty” goes on?

I just want my objects to spawn in a faster rate as time goes by. The print(SpawningInBetween); print out shows the percentage of the difficulty overtime.

This is the code I have:

public Vector2 spawnsMinMax;

//Spawn Positions
Vector2 SpawnPosition;
float secondsBetweenSpawn = 1;
float nextSpawnTime;

void Update()
{
if (Time.time > nextSpawnTime)
{
float SpawningInBetween = Mathf.Lerp(
spawnsMinMax.y,
spawnsMinMax.x,
Difficulty.GetDifficultyPercent());
print(SpawningInBetween);
nextSpawnTime = Time.time + SpawningInBetween;

nextSpawnTime = Time.time + secondsBetweenSpawn;
}

// ...


I was thinking of going like this:

if(Time.time > Difficulty.GetDifficultyPercent < 0.9)


but it got a compiler error.

• What exactly is that "error" you got? Perhaps we can tell you how to fix it. – Philipp Sep 18 '20 at 14:24
• it just got a red underline under "Time.time > Difficulty.GetDifficultyPercent < 0.9) – wq wq Sep 18 '20 at 14:26
• And what does Visual Studio say when you move your cursor on that red underline and wait for the tooltip to appear? – Philipp Sep 18 '20 at 14:27

You can control the spawn rate by changing the value of secondsBetweenSpawn to a value between 1.

One way to do that would be via script. For example, you can reduce the time at a rate of 10% every 5 seconds with this line in your Update function:

secondsBetweenSpawn = Mathf.Pow(0.9f, Time.timeSinceLevelLoad / 5f);


But another solution I find far more elegant is to control such game features with a Timeline asset. It allows you to define the progression of variables over the course of the game with curves which you can edit in a visual editor. Note that before you can control the value of a variable with a timeline, you need to make it public.

• no wait. your previous ones before you edit it actually kinda works.. is it ok to post your previous ones too hahaha. This works too but once i reset the game (in runTime i mean), it still spawns at the current spawn rate (which is by that time, it spawns alot) Thanks by the way. You really help – wq wq Sep 18 '20 at 14:43
• @wqwq How do you "reset the game"? If you do it by reloading the scene, then Time.timeSinceLevelLoad should work. But if you reset the game via a script which keeps the scene loaded but changes everything back to how its supposed to be, then you need to separately keep track of the time at which the game was restarted. – Philipp Sep 18 '20 at 14:46
• public class GameManager : MonoBehaviour { public void PlayGame() { SceneManager.LoadScene("FirstProject"); } } This is what i put. I'm sorry as i dont know what else to put it here haha – wq wq Sep 18 '20 at 14:52
• Then timeSinceLevelLoad should actually work. – Philipp Sep 18 '20 at 14:58