Update, Coroutines, and InvokeRepeating

Update is called every frame, but sometimes we hack in ways for the Update to be called less frequently than normal, and perhaps, without realizing it, we create a situation where an empty method is called more often than not:

void Update() {
  _timer += Time.deltaTime;
  if (_timer > _aiUpdateFrequency) {
    _timer -= _aiUpdateFrequency;

With this function definition, we are essentially calling an empty function almost every frame. In fact, it is worse than that; we're also performing a Boolean check that almost always returns false. This is fine if we don't abuse the concept, but as you've learned, having too many of these unnecessary function calls hiding in our Scene can be a sneaky hit on our ...

Get Unity 5 Game Optimization now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.