Is it possile to create a timer that set intervals each 40 seconds?

Hi,

I wonder if it is possible to create a timer where every 40 seconds I create an interval.

I have already tried the simplest way by writing " timer = 0 ", but this changes the internal environment and resets it to the initial state, I want to keep the last updates of the scene and create intervals.

I don’t know what you mean by

I can’t make much sense of that sentence ^^. If you’re looking for running a certain method at a certain inverval, you can use my CustomFixedUpdate class which implements the same logic Unity uses for it’s own FixedUpdate callback. The linked version on the wiki has some additional constructor overloads.

The simplest of approach would be to simply keep track of the passage of time.
When working with editors I rely on Time.realtimeSinceStartup, however for the actual game (in playtime) I rely on Time.deltaTime (or Time.unscaledDeltaTime if you intend to use timeScale). These two measure the lapse of time between two consecutive Update calls.

Bunny83’s FixedUpdate solution is also great because it measures a customizable interval that is likely to be very regular, however it will most likely be slightly out of sync with anything that happens in Update. When I say out-of-sync I don’t mean the error will accumulate and grow noticeable, it just means that things won’t always be perfectly aligned, but instead always within some small window of tolerance (depending on how the two frequencies mesh together). I personally don’t like this behavior, but the solution is still great because it’s robust, configurable, and reusable.

You can easily introduce just one such time tracking piece, and set up an event system to which any interested party can subscribe. Making this powerful, centralized, and scalable.

If a project is sufficiently small (or if it’s an editor), a static TimeTracker pattern is also ok, where you register a component, it gets some token back, and is then free to use all kinds of half-baked time-based methods. I had a blinking highlight in one of the editors I made recently for example, this was a great solution to obtain the properly-offset sinewaved opacity of the mesh without caring for anything time-based in the actual gizmo renderer.

you can also try:

Unless you want a headache in the long run, I would advise against quick and dirty solutions.
A one-liner now is half a dozen forum threads later.

1 Like