CW
Navigation
πŸ‡§πŸ‡· PT πŸ‡ΊπŸ‡Έ EN
Home About Stack Companies Blog
CONTACT
Back to Blog
24 Feb 2021 42 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

Full Stack Developer & Game Creator. Specialized in building immersive digital experiences and advanced systems.

Connect:
Share this article

Comments

Be the first to comment!

Leave a comment