Unity Run Code Once In Update | Unity Call Method Once In Update

Want to rune code once in update ? There are some methods for running a function once in Update() method. Sometime we make a mistake and run code in update method and our function start calling everyone frame. As the result of this game start lagging or may be unity editor crash.

unity run code once in update

Before starting tutorial, let us clear how this will work. First of all we define a bool and set as false. Now in update method we check that is our bool true or false. If our bool is false we call desired function and at same time, we set bool as true. Now let us see the code.

bool isFunctionCalled = false;

private void Update(){

if(isFunctionCalled == false){


isFunctionCalled = true;



private void MyFunction(){

// This function will be called only once in update




Uses of above function –

1) For checking countdown timer and showing result after countdown is over. Sometime we forget about update function and after countdown ends, update function start calling our function over and over. This may cause heavy lag and even Unity crash. So be careful.

As we know, update function is called every frame so if you call a function in update method, it may cause some problems. So we use funstions in update very carefully. Using above method, you can easily call any function once in update. Now if you want to know how it works then read following paragraphs.

At first we declare a bool called isFunctionCalled and leave it as false. Now in update method check is isFunctionCalled is false then call your desired function. And in next line set isFunctionCalled to true. Now at the next frame, isFunctionCalled is true so your function won’t be called. And hence your problem is solved.

Leave a Reply

Your email address will not be published. Required fields are marked *