Back to Blog
24 Feb 2021
44 Views
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! π
Comments
Be the first to comment!
