What timer logic should I user in my game?

Hello there,

I am trying to understand better the logic of timers and what is the best practice. Until now I used something like this: I set a waitTime, I increment a number in the update function, if the timer is above waitTime then the time is over.

waitTime = 10;	
void Update()
{
        timer ++;
        if (timer > waitTime)
        {
            timer = 0;
			Debug.Log("time is over!");
        }
}

I could use also Time.deltaTime to increment the timer variable. For what I understand the difference between this two practice is:
timer++ increment every frame by 1,
timer+= time.deltaTime increment every frame with the time passed between one frame and the previous.

In both case if my game is lagging the time will lag also.


If I put everything in the fixedUpdate() and i use Time.fixedDeltaTime my timer will increment regardless of my framerate, but I don’t think I want something like that. If I am using a timer to change the state of my player from poison to normal I think the time should be adjusted with the framerate…


As you see I grasp the idea, I would like to know what is the best practice in your opinion.

timer++; is never a good idea as it is not based on time taken.
Time.deltaTime will vary with framerate, and is usually ok to use.
FixedUpdate() and Time.fixedDeltaTime should be used to have the game behave consistent between different computers, for example in a multi-player game or a game where you want to have physics calculated the same, or want to be able to predict physics. But the Unity physics will run with fixed timestep and it isn’t something you can choose.

A timer just to see how much time has passed is often not critical which one you use.