Coroutine or InvokeRepeating?

Hi there !
I’d like to do a method that would Spawn boxes.

void Start()
    {
        InvokeRepeating("SpawnBalloon", 0, spawnRate);
    }
    void Update()
    {
        timeElapsed += Time.deltaTime;
        if (timeElapsed >= spawnRateIncreaseInterval)
        {
            spawnRate -= spawnRateIncreaseAmount;
            timeElapsed = 0f;
            CancelInvoke("SpawnBalloon");
            InvokeRepeating("SpawnBalloon", 0, spawnRate); 
        }
    }

I did this, but there’s an issue as it sometimes spawns a box, then changes the spawnRate and spawns another one right away.

I’ve solved this using Coroutines instead. (Thanks Claude !)

But I was wondering then, what’s really best ? When do you use InvokeRepeating, and when do you use Coroutines ?

Thanks a bunch !

Neither a coroutine nor Invoke. Just run a timer in Update, and spawn objects via that. You will have more control that way.

How would that work ? And what about InvokeRepeating and Coroutine ? I’m trying to understand how best to do stuff, but there are so many different ways it’s difficult as a beginner to understand why one way is better than another.

Roughly the same way you’re already doing the spawn interval timer in update.

That’s understandable. Invoke is a very old bit of the Unity API from its earlier days, and I’d advise against using it. Coroutines should only be used for fire-and-forget situations. If you need to stop and start something, regularly change its behaviour, you should be operating it on a per-frame basis with Update.