Is there a way of subscribing to Update() without the script being attached to a GameObject?

I’m only just beginning to learn about events in C-Sharp and I understand that Update() is an event. I want to create a class that I make lots of instances of, which update every frame. There seem to be only two ways of doing that: either instantiate them as prefabs, in which case my hierachy is filled with many gameobjects (which I, being a bit OCD don’t like!) - or, call all of their Update() methods from a proper GameObject’s Update() method.

But is it possible to derive my class from MonoBehaviour and somehow subscribe manually to the MonoBehaviour.Update() event, so that the instanced objects can update automatically every frame without the script being attached to a prefab?

No, the MonoBehaviour-base-class is derived from Component and therefore can only exist on GameObjects. You can’t create Components via “new”.

In editor-scripts there’s the update-delegate you can use, but unfortunately there is nothing similar for the runtime.

You still can derive your class from MonoBehaviour and even attach them to the same GameObject multiple times or just parent the GameObjects to an empty “folder”-GameObject so your hierarchy stays clean. Seperate components are easier to debug since you can simply make things public and view them in the inspector or add a temporary OnGUI to it.

However, i’m a friend of the custom-event-approach. I did a lot with custom interfaces or delegates. That’s also the reason why i hate SendMassage :D. Well it could be quite useful in some cases but i simply don’t like it. (string-based function calls are a no-go in my eyes… like the string-based GetComponent function they can’t be checked by the compiler)

Contrary to what @Bunny83 said, yes, you can do it.
I found 2 ways to do it:

  1. Inherit from Unity’s Entity Component System class: SystemBase overview | Entities | 1.3.8 . Then implement your events there before or after the normal ones.
  2. Create a custom game loop with the events that you want: How to create an early and a super late update in Unity - C# and Unity development

If you ask me, Unity developers should have done it themselves and not force us to use MonoBehavior and Unity messages, and instead should have defaulted to the default C# events pattern(GoF observer). And then on top of it, if people want, they can use the fancy Unity messages and just implement the functions in the class.

I have a valid use case for needing to get Update and LateUpdate messages in a non-MonoBehavior class and I can’t do it. I’ll implement one of those solutions, but this is something Unity developers should have done, not me, because duh: there are many use cases where you want to subscribe to all kinds of events and inheriting from MonoBehavior is unnecessary, uncomfortable and becomes a big chore.