Temporary Disabling Of Function Update?

Greetings All,

Hope that all your game creation activities are going well :smile:

What I’m wondering is-

What would you say is the best way to temporarily run code at function Update speeds, starting and stopping it at will? (i.e. To reduce the CPU usage of having the Update function constantly run, even if it’s not needed)

Is there anyway to deactivate a function Update without deactivating an entire object or removing a script?

Thanks for any suggestions!

Probably the best way is not to use Update at all, and use coroutines instead.

function DoStuff() {
    while (true) {
        // do stuff
        yield;
    }
}

Then you can use StartCoroutine and StopCoroutine as needed. Or you can use other logic instead of “while (true)” to make loops start and stop under certain conditions. I find that I actually tend not to use Update much these days, except for polling input.

Another method is to enable and disable scripts (not the same as activating/deactivating objects), which will cause the Update to stop working while the script is disabled. That’s usually messier though.

–Eric