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 performance.
This function is a perfect example of a function we can convert into a Coroutine to make use of their delayed-invocation properties:
void Start() { StartCoroutine(UpdateAI()); } IEnumerator UpdateAI() { while (true...