1
\$\begingroup\$

Im fairly new to unity development and I am working on repeating an enemy. I currently have the functionality working correctly in which I use invokerepeating to spam an enemy. Now, I have an if statement that drops a different enemy if a specific Item is hit. This functionality works in both invokerepeating and update. Could someone explain to me which one is more efficient? And why?

\$\endgroup\$

1 Answer 1

5
\$\begingroup\$

I would advise using neither for this particular case.

InvokeRepeating takes a method name, which means it's using reflection at runtime to look up the method to call. That's work we don't need to do, since we can already point the compiler at the exact method we want.

Update executes every frame, but for spawning we typically want to leave some time (or other condition) between spawns so things don't bunch-up. That means we're repeatedly calling the function just to do nothing until the right frame.

Neither of these is awful, but we can do better with coroutines. These let us set up recurring behaviours that execute only when we want them to, without any unnecessary string lookup complexity.

bool keepSpawning = true;

IEnumerator SpawnAtIntervals(float secondsBetweenSpawns)
{
    // Repeat until keepSpawning == false or this GameObject is disabled/destroyed.
    while(keepSpawning)
    {
         // Put this coroutine to sleep until the next spawn time.
         yield return new WaitForSeconds(secondsBetweenSpawns);

         // Now it's time to spawn again.
         Spawn();
    }
}

Then we can kick off the spawning with...

StartCoroutine(SpawnAtIntervals(3f)); // Or whatever delay we want.

And we can stop spawning by setting keepSpawning = false;

If you need more granular control (eg. the ability to switch between two spawning streams, etc.) this is possible too - you'll just need to include in your question more details about your use cases.

\$\endgroup\$
4
  • \$\begingroup\$ thank you this is very helpful. I saw something about using a counter as well. Would a coroutine be better than a counter also? \$\endgroup\$ Commented Apr 26, 2016 at 13:43
  • \$\begingroup\$ I'm not familiar with a context in which a counter does the same thing as these three methods, although one could use an integer variable as a counter to control the number of spawns in any of the three. Maybe there's another definition of "counter" you're referring to? \$\endgroup\$
    – DMGregory
    Commented Apr 26, 2016 at 13:49
  • \$\begingroup\$ Haha im sorry im an android developer but I meant time.deltatime (time class) \$\endgroup\$ Commented Apr 26, 2016 at 13:51
  • \$\begingroup\$ I don't see any methods of the Unity Time class that execute a custom recurring event. You can of course compare against the current time or accumulate deltaTime until some threshold, but then we're back in the Update situation, writing code whose job it is to say "I have nothing to do yet" instead of letting the engine handle that scheduling for us. In any case, this is starting to sound like a different question. If the above doesn't cover what you need, then try writing up the method you want to ask about and posting it as a new question. \$\endgroup\$
    – DMGregory
    Commented Apr 26, 2016 at 13:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .