Can you create a custom update-like function?

My project is using websockets to download data at irregular intervals. Multiple classes do recalculations every time this data is received, and only once per reception.

The current plan is to have the data transmitted to each of classes that need it when the data changed by attaching each of those scripts to the object that is receiving the data, or maybe some intermediate wrapper.

What would be ideal is to have a function similar to Update() that is called in each script when the data receive event happens. Is there a way to create a project wide function with that is called the same way?

Straightfoward answer:

You can use a Coroutine, with a while loop to perform loop like method, note that this implementation will run forever, you start it by calling StartCouritne(Routine()); .

    public IEnumerator Routine()
    {
        while(true)
        {
            //Do stuff.
            yield return null;
        }
    }

You can also pass a parameter to the while loop so it will only execute for as long as it’s true, or you can remove the while loop and use yield return new WaitForSeconds(/*float time*/); or even not use the WaitForSeconds and yield until another IEnumerator Method finishes.

For more info on Coroutines: Unity - Manual: Coroutines

Another answer based on what you described, i think you might need: (maybe i’m wrong)

You could use a static delegate to send an event when the data change, like:

public static event System.Action onDataChanged;

Call this event when the data changed like: onDataChanged(); or onDataChanged?.Invoke(); depends on the C# version you’re using.

And than subscribe each script that needs to know this happened, with:

onDataChanged += DoSomething; // You have to implemente the method DoSomething or whatever name you deem fit.

or

onDataChanged += { /* What it will do when the event occurs */ };