Find out where code is executed - in Update, in FixedUpdate or in LateUpdate

Hi!

Is there any built-in way to detect from code, in which lifecycle step that code is currently executed - in Update, FixedUpdate or LateUpdate?
I tried to google, but didn’t find an answer :frowning:

I doubt it. What are you trying to do?

Yeah, just Debug.Log.

If you pause the game, and click click the entry in the console log, it will show you a full stack trace of the calling functions, not just where it was called.

But any half decent IDE will let you see exactly where a function is called from anyway, so as above, what are you trying to solve?

2 Likes

I have helper, which can delay execution of provided function until next Update or FixedUpdate, and then execute that function there, in Unity thread.
For example, I use that helper for AdMob callbacks, which by some reason happen not even in Unity thread at all. Also I have multithreaded mesh generation in one place, and in order to apply mesh to gameobject I should do that in Unity thread.
That helper can be theoretically used anywhere, so I want it not to schedule function execution and just execute it straight away if by some reason it was requested in the same Update or FixedUpdate.

Currently I solved that in quite an ugly way - I created simple monobehavior which just stores, which Unity function was executed last - Update, FixedUpdate or LateUpdate. And in project settings I set execution order for that behavior to big negative number, so it will be executing before everything else.

There’s already a free asset that lets you marshal tasks from coroutines to background threads and back to the main thread when required.

If you want to copy that behavior, I’d start there, since that tool works perfectly.

1 Like

There is Time.inFixedTimeStep
It might partially solve your problem.

1 Like