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) {
    ProcessAI();
    _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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.