Unity architecture

I have started learning Unity recently. I have some questions (maybe a bit premature) about its design.

First, why there exists the SendMessage method? Why use reflection when you can just use a simple observer pattern and limit the number of receivers to ones that implement an interface?
Second, how trigger callbacks (OnEnterTrigger etc) are getting called if I dont use override? Again, why is it not implemented by listeners?

And finally, what could you advise me to read on Unity architecture and games design with unity?

Totally not an expert here, but I agree with challenging SendMessage (and so do many others, I believe). I’ve used Unity for a year or two, and never needed to use SendMessage.

Don’t really understand your question about the triggers…

Tons of youtube videos out there, I got started with the infiniteammo tutorials (just search), but you seem to have a more solid programming background than I did when I started so there might be some better starting point for you. A good intro though.

And the docs and example projects are actually really good, will take you a long way both architecture- and otherwise.

Unity tried to make things easy for beginners and decouple components. The observer pattern is nice and dandy but it requires more setup work on the user side. Sometimes it’s a pain to get the correct reference of a nested component. SendMessage is a generic way to execute methods on other components.

Unity callbacks (Update, Start, OnTriggerEnter, …) are ordinary methods but the invocation is highly optimised. As you might know the core of Unity is written in native code, mostly C++. ´Mono is “just” used as scripting interface. Unity analyses your classes during compilation / loading and probably holds some kind of flags / invocation lists for each possible callback. So Unity only executes callbacks that are actually implemented. When using virtual methods all possible callbacks would be required to be executed. That would be a huge overhead. The way they implemented it, the engine only executes callbacks that are implemented.

Unity invokes all callbacks from native code. So there’s always the transition overhead between native <--> managed code. SendMessage uses some kind of reflection internally. How it’s implemented in detail we don’t know, but it’s quite optimised. Of course it’s some magnitudes slower than using a direct method call, but it most cases it doesn’t matter. It also doesn’t force you to use it. A lot people use their own messaging system. Check out the wiki there are a lot messaging systems available for free :wink:

SendMessage is called on the next Update. So unlike simple method call it is delayed by one frame. It can be used if you want to accumulate messages from multiple objects or handle message after all objects are updated.

You should read Unity - Manual: Order of execution for event functions