unity call function on update once per second 1

unity call function on update once per second

float elapsedTime;
float timeLimit = 0.1f;

private void Update()
{
    elapsedTime += Time.deltaTime;
    if (elapsedTime >= timeLimit)
    {
        elapsedTime = 0;
        //Rest of your code or function call goes here
    }
}

Here is what the above code is Doing:
1. We create a float variable called elapsedTime and set it to 0.
2. We create a float variable called timeLimit and set it to 0.1f.
3. In the Update() function, we add the Time.deltaTime to the elapsedTime variable.
4. We check if the elapsedTime is greater than or equal to the timeLimit.
5. If it is, we set the elapsedTime to 0 and call our function or run our code.

Similar Posts