Does Unity have any engine callbacks that a script can hook into (like Update.)

I am looking for a way to get a non-MonoBehavior script instance to run every frame. Currently I am handling this by calling a function in the script from a monobehaviors Update() function, but I’d like to get rid of the need for a script on a game object in the scene. Does Unity have any engine callbacks that my script can hook into or some method of getting my function to execute every frame?

No.

Unfortunately appels is correct. MonoBehaviour is an absolute requirement to get any Update() callbacks.

You can have a single ‘callback manager’ or ‘scene graph manager’ which offers delegate lists and fires them.
that way you can use a single monobehaviour to handle all non-monobehaviours.

Why one would want to do that though is beyond me, as it cuts the Active and Hierarchy handling as well as the whole Component based, iterative development focused workflow, which is basically the very core of the engine and I personally would recheck if the engine is suitable for your needs any longer if you want to forcefully get rid of it.

The only callback to surely get rid off from multiple monobehaviours at least is OnGUI due to its overhead.

You could do something like this in a monobehaviour, then all you need to do is register to it

using System;
using UnityEngine;

public sealed class UpdateRunner : MonoBehaviour {
private static event Action UpdateTarget;

public static void Register(Action method){
    UpdateTarget += method;
}

public static void Unregister(Action method){
    UpdateTarget -= method;
}

private void Update(){
    if (UpdateTarget != null)
    {
        UpdateTarget();
    }
}

Then you simply register your non monobehaviour update like so:

UpdateRunner.Register(DoUpdate);

Not sure why you would do this, but this code works.

Well it’s actually very handy for global game logic “managers”, basically singletons. We use one GameManager game object that manages all these classes. This is required to keep some global game state intact when you have your levels changing (or at least the only one I’ve found to actually work nicely).

Actually…why wouldn’t this be a good idea?

You could have many event subscribers that don’t require inheriting monobehavior, that all get fired from a single Update. Maybe they would use frame or DeltaTime values from the original update function for calculations, I dunno. But it seems like using plain .net classes to do the work, instead of a Monobehavior object, would be better.

Wow, lot of replys! Thanks folks.

It sounds like some others have nailed the purpose on the head. I want to write singleton managers that don’t need to be on a game object in the scene. In this particular case, I have coded an Invoke() functionality that is not affected by timescale, which means I need to loop through a list of delegates, and check their associated fire time against the unity’s Time.realTimeSinceStartup. This must be checked on a constant basis, and an OnUpdate callback would be ideal.

Right now, I am calling the managers Update() function from a monobehavior, but it just feels dirty :slight_smile:

I know this is super old, but it’s worth noting that in the editor, there is something that exists that does exactly what you want: EditorApplication.update - see Unity - Scripting API: EditorApplication.update

It’s a shame nothing like this exists during play, but that’s just Unity for you :slight_smile:

3 Likes