c# – Unity: how NOT to call Update() if [condition]?

In Global space I would have:

public enum UpdateMode{
EveryFrame,
FixedUpdate

}

Then in my script: UpdateMode updateMode;

private void Update()
{
    if (updateMode != UpdateMode.EveryFrame) { return; }
    MyFunction();
}

private void FixedUpdate()
{
    if (updateMode != UpdateMode.FixedUpdate) { return; }
    MyFunction();
}

But I still don’t like the idea that the Update() function is still called every frame, this screams unoptimized. I thought about making 2 separate scripts for each case, but a lot of code would repeat, it would be just coping the script, renaming it and Writing Update/FixedUpdate. I also thought about making 2 more scripts, one with Update() and one with FixedUpdate() and then just call MyFunction() via reference.

I wanted to know if there was a more elegant way to approach this or which solution would be more performant, both in CPU cycles, memory and complexity.

Thanks anticipated.

Read more here: Source link