Skip to content
CW
Navigation

Language

Navigation

Back to Blog
24 Feb 2021 1 min read 1479 Views
Execute an operation each X frames on Unity Update() Method

Execute an operation each X frames on Unity Update() Method

Optimize Unity's Update()! Run code every X frames using Time.frameCount % X. Improve performance by avoiding unnecessary calculations.

Often you want the ease of using Unity's Update() method, but want to avoid performing a heavy operation on all frames when that is not always necessary?

Here's a golden tip!

void Update()
{
    if (Time.frameCount % 10 == 0)
   {
      //This will be only executed each 10 frames
   }
}

You can change the value 10 to the interval of frames that you want to this code block to be executed.

Did you already know this feature? I use it in several of my games to avoid performing operations on all frames.

Until next time, peace! πŸ––

Cezar Wagenheimer

Written by

Cezar Wagenheimer

Share this article

Comments

Have a question, or found an issue with the code? Drop a comment below — I read and reply to every one.

Loading comments…

Leave a comment

Keep exploring