General question: Accessing variables vs Sending Message

I realize the answer might be “it depends” , or impossible to answer in general terms.

But…

Are there any performance differences between scripts using SendMessage() and directly accessing variables in other scripts?

What about compiling times? I use js and it’s taking me about 30 seconds every time I compile. Would it compile faster if every script wasn’t accessing each other, but using SendMessage instead?

Every object and character has over 10 or 20 scripts that handle physics, movement, ai, effects, and they communicate with each other by directly accessing public variables.

Would it be better to use SendMessage functions? Or about the same?

SendMessage is much slower than directly accessing variables. It’s also considerably more dangerous.

These aren’t your only two options, though. There are Unity Events, the Unity Messaging system, and good old-fashioned calling of methods on the other object. In my opinion there is never a good reason to use SendMessage any more.

I don’t believe that use of SendMessage has any impact at all on compiling times. Switching to C# might improve that, but I’m not sure; there are so many other reasons to switch to C# that I’ve never before considered this one. :slight_smile:

1 Like

Accessing public variables is faster than SendMessage. Since SendMessage uses a string to locate the method to call, it probably uses reflection. Reflection is about 1000 times slower than the same straight function call.

1 Like

SendMessage uses reflection, therefore is technically slower.

Neither effect compile time. Amount of code effects compile time. And also, because unity actually compiles numerous different libraries of code (js and C# code, plugins js and C#, code in editor folders, etc). This is what effect compile time (though technically we won’t get into the semantics of the word ‘compile’).

I don’t see the correlation between ‘accessing public variables’ and ‘SendMessage’. SendMessage is more for signaling an event of some sort. Where as accessing a public member is just how you retrieve data… it’s not an event (though you technically could retrieve a representation of an event, like UnityEvent or .Net Event, so you can register for when event occurs).

Getting the velocity of a Rigidbody, vs receiving the ‘OnCollisionEnter’ message for that same Rigidbody, are completely different things all together. How would you ‘access a public variable’ to know that there was a collision for the Rigidbody?

So really, I’m having a hard time figuring out what you’re attempting to get at?

1 Like

This gives me a peace of mind, guys. Thank you. I’ll keep doing what I’m doing, I’ve been told C# has faster compile times, switching languages is in my to do list.

Well, say one script requires to activate some effect, animation, or whatever in another script. I can either access the other script directly using GetComponent, and change a variable directly, or a run a function directly; or I could use a SendMessage to call a function passing a variable, and activate the effect that way, which would work on any receiver that has that function name.

My logic was, since SendMessage sends the “message” to receivers without requiring a variable that access another script, I would avoid having a lot of global variables to access all the different scripts, for every script, and this could somehow help compiling times.