How to know if function is running in Update() or FixedUpdate()?

From the docs on Time.deltaTime:

I would like to do something similar. I have a static float that I would like to return different values based on if the float gets called during Update() or FixedUpdate(). Is there a way to tell which is currently running?

Alternatively, is there an FixedUpdate() equivalent to Time.frameCount?

1 Like

Well, one hackish way would be to compare Time.deltaTime to the fixed delta time. If they’re the same, you’re in FixedUpdate. (It’s also likely that you could simply use Time.deltaTime directly)

2 Likes

You could make use of a simple bool variable, if everything is in the same behaviour, or send it to your function.

A bool is a pretty light variable, so it shouldn’t impact performance much, even if it is needed everywhere.

I don’t know of anything in the unity framework that is publicly available.

The update loop is set up nicely though that you could hack out a way to track if you’re in that.

Checking the Execution Order:

You’ll see that the update loop starts with calling ‘Update’, deals with all the coroutine stuff, and then calls ‘LateUpdate’.

You can exploit the execution order and have 2 scripts that handle ‘Update’ and ‘LateUpdate’. Set one of the scripts to be the first ever to execute, and set the other to be the last script to be executed.

Now in the early script, on ‘Update’, set a boolean flag. And in the late script in ‘LateUpdate’ unset the boolean flag. Now if this flag is true, we know that we’re somewhere in between the update and lateupdate.

Unfortunately FixedUpdate in the execution order does not have a ‘latefixedupdate’ to know we’re exiting the FixedUpdate part of the execution order. As far as I can tell the calls to FixedUpdate and Update don’t overlap, I don’t know if you can really rely that the order will remain as documented above.

You may be able to have an ‘early’ executing script track when you’ve entered FixedUpdate though and then set that back False when the early ‘Update’ is called. This means that the mouse events will return true for being in the FixedUpdate cycle, even though they aren’t, it still might work. Give it a try…

Though I don’t track these bools. I do actually have a static class in my spacepuppy framework (it’s marked as internal) for tracking this ‘early’ and ‘tardy’ update and fixed update. The static class is then called ‘GameLoopEntry’ that allows me to hook into the update loop from classes that aren’t scripts.

The components that have their execution order set:
https://code.google.com/p/spacepuppy-unity-framework/source/browse/#svn/trunk/SpacepuppyUnityFramework/Hooks

[EDIT]
I just commited an update to it that tracks entry and exit.

I also thought about a boolean like @slay_mithos but a little different, as you could set a boolean and play around with execution orders. In the property check whether it’s true ot not and return the appropriate value.

Since Unity 5.6, you can use Time.inFixedTimeStep

3 Likes